Akka-http https呼叫返回403

时间:2018-06-01 08:19:06

标签: scala akka akka-http

我正在尝试拨打此电话

curl -X POST \
 'https://structuredproducts-ch.leonteq.com/engine- 
 api/feed/timeseries/request?from=201805310000&to=201805311602' \
 -H 'accept: application/json' \
 -H 'content-type: application/json' \
 -d '{  "sophisInternalIds": [    67108959  ]}'

并且通过邮递员或卷曲在当地正常工作但是使用akka-http我无法通过。

尝试手动接受每个SSL主机名验证,但这也无济于事。永远得到403禁止。

当我尝试其他一些技巧时,我最终得到SSLEngine问题,这再次导致我无处可去。

private val trustfulSslContext: SSLContext = {

   object NoCheckX509TrustManager extends X509TrustManager {
     override def checkClientTrusted(chain: Array[X509Certificate], authType: String): Unit = ()

     override def checkServerTrusted(chain: Array[X509Certificate], authType: String): Unit = ()

     override def getAcceptedIssuers: Array[X509Certificate] = Array[X509Certificate]()
   }

   val context = SSLContext.getInstance("TLS")
   context.init(Array[KeyManager](), Array(NoCheckX509TrustManager), null)
   context
}


  Http(context.system)
    .singleRequest(
      HttpRequest(
       uri = Uri(url), 
       method = HttpMethods.POST, 
       entity = "lala", 
       protocol = HttpProtocols.`HTTP/1.1`),
      connectionContext = ConnectionContext.https(trustfulSslContext)
    )

我正在使用akka-http 10.1.0。

1 个答案:

答案 0 :(得分:3)

原来你需要传递正确的Content-Type标题和数据。我想它是如何在该服务器中实现的。因此,以下代码有效:

val requestEntity: RequestEntity = HttpEntity.Strict(
    contentType = ContentTypes.`application/json`,
    data = ByteString("{  \"sophisInternalIds\": [    67108959  ]}")
)

Http(context.system)
    .singleRequest(
    HttpRequest(
        uri = Uri("https://structuredproducts-ch.leonteq.com/engine-api/feed/timeseries/request?from=201805310000&to=201805311602"),
        method = HttpMethods.POST,
        entity = requestEntity,
        protocol = HttpProtocols.`HTTP/1.1`
    )
)