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

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

问题描述

网络环境:

Network environment:


Https客户端< =============>代理服务器< ==============> Https服务器

             ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 192.168.17.11< -----外联网------> 192.168.17.22 <无线电通信/>
10.100.21.10< ---- intranet -----> 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客户端,但它可以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 Server:Tomcat6.Have one network-interface。

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

(为方便我不要记下用于检查 serverTrusted hostnameVerifier 问题的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属性时方法:

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:网络无法访问

这让我很困惑。我没有在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:网络无法访问

我认为这是合理的。因为没有到外联网的路线。

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.

我想似乎 setProperty 方法 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.

推荐答案

您的网址连接是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天全站免登陆