如何使 HTTPS(不是 HTTP)请求使用 akka-http?

How to make HTTPS (not HTTP) request use akka-http?

我正在使用以下代码通过 Akka Actor 内部的 akka-http 库发出 HTTP 请求:

implicit val materializer = ActorFlowMaterializer()
implicit val system = context.system

val request = HttpRequest(HttpMethods.GET, "http://ya.ru")
val content = for {
  response <- Http().singleRequest(request)
  content <- Unmarshal(response.entity).to[String]
} yield content

一切正常,但现在我想发出 HTTPS 请求(只需将 http:// 替换为 https://)。之后 content 变量将包含以下响应:

<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx</center>
</body>
</html>

好像akka-http不支持HTTPS协议。使用 akka-http 发送 HTTPS 请求是否正确或可能?

如果您查看 official documentation,您会发现必须配置 HTTPS 上下文。然后它应该工作。