使用Finagle Http客户端进行https请求

时间:2017-11-16 04:09:32

标签: scala web-services finagle

我正在尝试从REST Web服务获取一些数据。到目前为止,如果我不使用HTTPS,并且此代码按预期工作,我可以正确获取数据 -

val client = Http.client.newService(s"$host:80")

val r = http.Request(http.Method.Post, "/api/search/") 
r.host(host)    
r.content = queryBuf
r.headerMap.add(Fields.ContentLength, queryBuf.length.toString)
r.headerMap.add("Content-Type", "application/json;charset=UTF-8")

val response: Future[http.Response] = client(r)

但是当我尝试从https请求获取相同数据时(遵循此link

val client = Http.client.withTls(host).newService(s"$host:443")

val r = http.Request(http.Method.Post, "/api/search/")
r.headerMap.add("Cookie", s"_elfowl=${authToken.elfowlToken}; dc=$dc")
r.host(host)    
r.content = queryBuf
r.headerMap.add(Fields.ContentLength, queryBuf.length.toString)
r.headerMap.add("Content-Type", "application/json;charset=UTF-8")
r.headerMap.add("User-Agent", authToken.userAgent)

val response: Future[http.Response] = client(r)

我收到错误

Remote Info: Not Available at remote address: searchservice.com/10.59.201.29:443. Remote Info: Not Available, flags=0x08

我可以使用443端口卷曲相同的端点,并返回正确的结果。任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

很少有事情要检查:

withTls(host)

需要是服务器证书中的主机名(例如,与ip相对)

你可以尝试:

Http.client.withTlsWithoutValidation

验证上述内容。

此外,您可能需要验证服务器是否检查主机标头是否已设置,如果是,您可能希望包含它:

val withHeader = new SimpleFilter[http.Request, http.Response] {
  override def apply(request: http.Request, service: HttpService): Future[http.Response] = {
    request.host_=(host)
    service(request)
  }
}

withHeader.andThen(client)

有关主机标头的更多信息: What is http host header?