【发布时间】:2018-11-16 07:52:39
【问题描述】:
我正在尝试使用 JDK 11 HttpClient 通过公司代理发出请求,该代理需要通过登录名和密码进行身份验证。根据JDK的intro,我正在通过以下方式构建客户端实例:
HttpClient httpClient = HttpClient.newBuilder()
.version(HTTP_1_1)
.proxy(ProxySelector.of(new InetSocketAddress("proxy.mycompany.com", 3128)))
.authenticator(authenticator)
.build();
,其中authenticator 是:
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("login", "password".toCharArray());
}
};
然后我自己执行请求:
HttpRequest outRequest = HttpRequest.newBuilder()
.version(HTTP_1_1)
.GET()
.uri(URI.create("https://httpwg.org/asset/http.svg")) // no matter which URI to request
.build();
HttpResponse<String> inResponse = httpClient.send(outRequest, BodyHandlers.ofString());
但我收到 HTTP 407(需要代理身份验证),而不是来自目标服务器 (https://httpwg.org) 的有效响应,即 HttpClient 不使用提供的 authenticator。
我尝试了here 和here 提到的各种解决方案,但都没有帮助。
什么是让它工作的正确方法?
【问题讨论】:
-
似乎在访问http站点时有效,但不是https。你找到解决办法了吗?
-
@Keijack,对于那个特定的应用程序,我不得不使用 Apache HTTP 客户端,因为它是唯一能够处理 NTLM 身份验证的客户端。但我仍然想知道如何使它与 JDK HttpClient 一起使用。
标签: java http-proxy java-11 java-http-client