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

hrirmatl  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(145)

Akka-http版本-10.0.14
Akka版本-2.4.20

    • 使用akka http作为客户端发送GET请求并接收响应正文时出现以下错误。**

线程"main"中的异常akka. http. impl. engine. client.传出连接蓝图$意外连接关闭异常:http服务器在传递1个未完成请求的响应之前意外关闭了连接
观察结果:
1.通过curl命令执行相同的GET调用
1.相同的客户端代码可用于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()
  }
}
jtjikinw

jtjikinw1#

当我使用0.0.0.0或实际IP地址作为主机地址时,get调用按预期工作!不知道“localhost”有什么问题!

相关问题