如何通过 setProperty 通过代理使用 HttpsURLConnection? [英] How to use HttpsURLConnection through proxy by setProperty?

查看:41
本文介绍了如何通过 setProperty 通过代理使用 HttpsURLConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络环境:

Https 客户端<============>代理服务器<============>Https 服务器
                                192.168.17.11<-----外网------>192.168.17.22
10.100.21.10<----内网----->10.100.21.11

Https Client<=============>Proxy Server<==============>Https Server
                                                  192.168.17.11<-----extranet------>192.168.17.22
10.100.21.10<----intranet----->10.100.21.11

ps: Http Client 没有默认网关,但是可以 ping 到 10.100.21.11

ps: Http Client without default gateway, but it can ping to 10.100.21.11

说明:

操作系统:3 台主机上的 Ubuntu 12.04
Https 客户端:用 java(openjdk-6) 实现.有一个网络接口.
代理服务器:Apache2.2.有两个网络接口.
Https 服务器:Tomcat6.有一个网络接口.

OS: Ubuntu 12.04 on 3 hosts
Https Client: Implement with java(openjdk-6).Have one network-interface.
Proxy Server: Apache2.2.Have two network-interfaces.
Https Server: Tomcat6.Have one network-interface.

我使用两种方法通过代理实现httpsurlconnection:
(为了方便我没有写关于检查serverTrustedhostnameVerifier问题的ssl句柄函数.如果需要我会更新.)

I use two method to implement httpsurlconnection through proxy:
(For facilitate I do not write down about ssl handle function for checking serverTrusted and hostnameVerifier issue.If need I will update.)

InetSocketAddress proxyInet = new InetSocketAddress("10.100.21.11",80);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyInet);
URL httpsUrl = new URL("https://192.168.17.22:8443/test");
HttpsURLConnection httpsCon = (HttpsURLConnection) httpsUrl.openConnection(proxy);

httpsCon.setDoOutput(true);
httpsCon.setDoInput(true);
httpsCon.setRequestMethod("POST");
OutputStream out = httpsCon.getOutputStream();
OutputStreamWriter owriter = new OutputStreamWriter(out);

owriter.write("<request>test</request>");
owriter.flush();
owriter.close();
...

这个方法可行,我观察到的数据包流也符合我的预期.
HttpClient ---> ProxyServer ---> HttpServer

This method workable and I observed packets flow also met my expectation.
HttpClient ---> ProxyServer ---> HttpServer

但是当我使用 set Property 方法时:

But when I use set Property method:

System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost",10.100.21.11);
System.setProperty("http.proxyPort","80");

URL httpsUrl = new URL("https://192.168.17.22:8443/test");
HttpsURLConnection httpsCon = (HttpsURLConnection)httpsUrl.openConnection();

httpsCon.setDoOutput(true);
httpsCon.setDoInput(true);
httpsCon.setRequestMethod("POST");
OutputStream out = httpsCon.getOutputStream();
OutputStreamWriter owriter = new OutputStreamWriter(out);

owriter.write("<request>test</request>");
owriter.flush();
owriter.close();
...

我收到一个 NoRouteToHostException: Network is unreachable.
这让我很困惑.我没有看到 HttpClient 和 ProxyServer 之间的任何数据包.
但是HttpClient可以ping到ProxyServer(10.100.12.10 ping 10.100.21.11)

I got a NoRouteToHostException: Network is unreachable.
It make me confused.I did not see any packets between HttpClient and ProxyServer.
But HttpClient can ping to ProxyServer(10.100.12.10 ping 10.100.21.11)

所以我删除了代理设置(如不使用代理):
还得到 NoRouteToHostException: Network is unreachable.
我认为这是合理的.因为没有通往外网的路径.

So I remove proxy setting(as without using proxy):
Also got NoRouteToHostException: Network is unreachable.
I thought this is reasonable.Because there is no route to extranet.

我猜httpsUrlConnection的内部函数会检查这个url是否可达.

I guess it seems like to setProperty method that the inner function of httpsUrlConnection will to check this url can be reachable or not.

但这很奇怪.第一种方法可以成功.

But it is weird. 1st method can be success.

有什么想法吗?或者第一种方法和第二种方法有什么不同?

Have any idea? Or what are different between 1st and 2nd method?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

更新

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Update

System.setProperty("https.proxyHost",10.100.21.11);
System.setProperty("https.proxyPort","80"); 

它可以工作,并且数据包流量符合我的预期.
但是设置 https.proxyPort=443 对我不起作用

It can work and packets flow are correct what I expect for.
But set https.proxyPort=443 is not workable for me

System.setProperty("https.proxyPort","443");

它会抛出一个异常如下:

It will thorow a exception as bellow:

java.net.SocketException: Unexpected end of file from server 
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:770)
....

所以我认为 Apache Proxy 也必须修改为正确的配置.

So I thought Apache Proxy have also to be modified to the right configuration.

推荐答案

你的 URL 连接是 https 而你只是设置了 http 代理.

Your URL connection is https whereas you are only setting the http proxy.

尝试设置 https 代理.

Try setting the https proxy.

//System.setProperty("https.proxySet", "true"); 
 System.setProperty("https.proxyHost",10.100.21.11);
 System.setProperty("https.proxyPort","443");

编辑@EJP 是正确的.没有 https.proxySet .. 我复制了您的原始问题并包含在答案中.

EDIT @EJP is correct. There is no https.proxySet .. I copied your original question and included in the answer.

这篇关于如何通过 setProperty 通过代理使用 HttpsURLConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆