网络环境:
Https客户端<=============>代理服务器<==============> Https服务器192.168.17.11 192.168.17.2210.100.21.10 10.100.21.11
ps:没有默认网关的Http客户端,但是它可以ping到10.100.21.11
说明:
OS:3台主机上的Ubuntu 12.04Https客户端:使用java(openjdk-6)实现。具有一个网络接口。代理服务器:Apache2.2。具有两个网络接口。Https服务器:Tomcat6。具有一个网络接口。
我使用两种方法通过代理实现httpsurlconnection:(为方便起见,我不写下有关检查serverTrusted和hostnameVerifier问题的ssl句柄功能的信息。如有需要,我将进行更新。)
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
但是当我使用set Property方法时:
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)
因此,我删除了代理设置(因为不使用代理):也得到了NoRouteToHostException: Network is unreachable
。我认为这是合理的。因为没有通往Extranet的途径。
我想似乎是[[setProperty方法,httpsUrlConnection的内部函数将检查此URL是否可以访问。
但是这很奇怪。第一种方法可能会成功。有什么想法吗?还是第一方法和第二方法有什么不同?
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
更新
System.setProperty("https.proxyHost",10.100.21.11);
System.setProperty("https.proxyPort","80");
它可以正常工作,并且数据包流动符合我的期望。但是设置https.proxyPort = 443对我来说不可行]
System.setProperty("https.proxyPort","443");
它将抛出以下异常:
java.net.SocketException: Unexpected end of file from server at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:770) ....
所以我认为还必须将Apache Proxy修改为正确的配置。
尝试设置https代理。
Proxy
对象。创建一个如下: