核心Java HTTPClient可以通过TLS代理代理吗?
我正在尝试通过HTTPS身份验证的代理设置httpclient来代理请求。
Java HTTPClient似乎只能超过http
,因此代理凭据以授权发送给代理。
我已经在端口8443上打开了代理以接受http
,并且这效果很好:(
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
String user = "username";
String password = "password";
Authenticator authenticator = new ProxyAuthenticator(user, password);
Authenticator.setDefault(authenticator);
HttpClient httpClient = HttpClient.newBuilder()
.sslContext(getSSLContextTrustAny())
.version(HttpClient.Version.HTTP_1_1)
.proxy(ProxySelector.of(new InetSocketAddress("my.proxy", 8443)))
.authenticator(Authenticator.getDefault())
.build();
String uri = "https://ensc1aqsjv0asda.x.pipedream.net/";
String msg = "test-msg";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.POST(HttpRequest.BodyPublishers.ofString(msg))
.build();
HttpResponse<?> response = httpClient.send(request, HttpResponse.BodyHandlers.discarding());
System.out.println(response);
System.out.println(response.headers());
注意:getsslcontexttrustany()
返回一个信任任何证书的SSLContext 但是,签名的Ca)
但是,当我将端口设置为443(打开的端口)时,请求就会出来。我怀疑客户端试图代理http://my.proxy:443
,而不是tls to https://my.proxy:443
是可能的使客户使用https
用于代理?
I am trying to setup a HttpClient to proxy requests through a HTTPS authenticated proxy.
It seems the Java HttpClient can only proxy over http
and therefore the proxy credentials are sent in plaintext to the proxy.
I have opened the proxy on port 8443 to accept http
and this works fine:
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
String user = "username";
String password = "password";
Authenticator authenticator = new ProxyAuthenticator(user, password);
Authenticator.setDefault(authenticator);
HttpClient httpClient = HttpClient.newBuilder()
.sslContext(getSSLContextTrustAny())
.version(HttpClient.Version.HTTP_1_1)
.proxy(ProxySelector.of(new InetSocketAddress("my.proxy", 8443)))
.authenticator(Authenticator.getDefault())
.build();
String uri = "https://ensc1aqsjv0asda.x.pipedream.net/";
String msg = "test-msg";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.POST(HttpRequest.BodyPublishers.ofString(msg))
.build();
HttpResponse<?> response = httpClient.send(request, HttpResponse.BodyHandlers.discarding());
System.out.println(response);
System.out.println(response.headers());
(Note: getSSLContextTrustAny()
returns an SSLContext that trusts any cert as the proxy is using a self signed CA)
However, when I set the port to 443 (which is open), the request just times out. I suspect the client is trying to proxy to http://my.proxy:443
instead of over TLS to https://my.proxy:443
Is it possible to make the client use https
for proxying?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
否 - 这不支持。可以通过与代理的明确连接隧道连接到服务器的HTTPS连接,但是不可能通过加密的连接到代理,因此无法将清晰的HTTP连接到服务器。
No - this is not supported. It is possible to tunnel an HTTPS connection to a server through a clear connection to a proxy, but it is not possible to proxy a clear HTTP connection to a server through an encrypted connection to a proxy.