Akka http,GET Http()。singleRequest意外抛出服务器关闭的连接

时间:2018-11-30 15:34:00

标签: akka akka-http

Akka-http版本-10.0.14

Akka版本-2.4.20

使用akka http作为客户端发送GET请求并接收响应正文时遇到错误。

线程“ main”中的异常akka.http.impl.engine.client.OutgoingConnectionBlueprint $ UnexpectedConnectionClosureException:http服务器在为1个未完成的请求传递响应之前意外关闭了连接

观察:

  1. 相同的GET调用通过curl命令起作用

  2. 相同的客户端代码适用于GET调用“ https://api.xforce.ibmcloud.com/hub/invalidcall

代码-

import akka.actor._
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.util.Timeout
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._

object Client {

  implicit val timeout: Timeout = Timeout(10.seconds)

  def main(args: Array[String]): Unit = {

    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    implicit val executionContext = system.dispatcher

    val responseFuture: Future[HttpResponse] =
      Http().singleRequest(
        HttpRequest(
          uri = "http://localhost:9090/xstreams/v1/offset/getOffset?appId=local-1543580292487&sourceName=mysql",
          //uri="https://api.xforce.ibmcloud.com/hub/invalidcall",
          method = HttpMethods.GET
        )
      )

    val entity = Await.result(responseFuture, timeout.duration).entity
    val responseStringF = Unmarshal(entity).to[String]
    val responseString = Await.result(responseStringF, timeout.duration)

    println("\nResult = " + responseString)

    system.terminate()
  }
}

0 个答案:

没有答案