![](/img/trans.png)
[英]Proxy-Authorization header is not send within first request in Apache HttpComponents
[英]Using Proxy Authorization Header with ScalajHTTP
使用scalajHTTP时出现407错误。 我通读了存储库,看来我应该能够将基本身份验证凭据作为base64编码的值传递。 我还尝试使用GitHub问题.proxyAuth中描述的帮助器方法,但是根据错误消息(以及它不在文档中),它不再是ScalaJ中HTTPRequest的一部分。
有任何想法吗? 我的端点URL是HTTPS以及代理(用于其他上下文)
val proxyHost= s"https://$forwardProxy"
val requestForward = Http(url).postData(redactedSecret)
.option(HttpOptions.allowUnsafeSSL)
.headers(("Content-Type", "application/json"), ("Proxy-Authorization", s"Basic $proxyAuth"))
.proxy(proxyHost, 8080).asString
val responseForward: HttpResponse[String] = requestForward
此发行发布在Github上,但仍未解决, https://github.com/scalaj/scalaj-http/issues/87
我找到了解决此问题的方法。 在尝试使用HTTP客户端库后,我进行了研究,即使它们都支持代理身份验证,我仍然收到407错误。 无论如何,我最终不得不执行以下操作。
加
import java.net.{Authenticator,PasswordAuthentication}
我上面上面的修改后的代码主体如下所示:
val requestForward: HttpRequest = Http(url).postData(data)
.header("Content-Type", "application/json")
.proxy(proxyHost, 8080)
.option(HttpOptions.allowUnsafeSSL)
Authenticator.setDefault(new Authenticator() {
override def getPasswordAuthentication(): PasswordAuthentication = {
new PasswordAuthentication( s"$username", s"$password".toCharArray())
}
})
如您所见,我从原始请求对象中删除了标头,而是覆盖了凭据。 在调用响应对象之前,请确保已执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.