title: How to use HttpsURLConnection through proxy by setProperty?
body: <p>Network environment: <br/></p>
  
  <blockquote>
    <p>Https Client&lt;=============>Proxy Server&lt;==============>Https Server<br/>
    &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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;192.168.17.11&lt;-----extranet------>192.168.17.22<br/>
    10.100.21.10&lt;----intranet----->10.100.21.11</p>
    
    <p>ps: Http Client without default gateway, but it can ping to 10.100.21.11</p>
  </blockquote>
  
  <p>Description:<br/></p>
  
  <blockquote>
    <p>OS: Ubuntu 12.04 on 3 hosts<br/>
    Https Client: Implement with java(openjdk-6).Have one network-interface.<br/>
    Proxy Server: Apache2.2.Have two network-interfaces.<br/>
    Https Server: Tomcat6.Have one network-interface.<br/></p>
  </blockquote>
  
  <p>I use two method to implement <em>httpsurlconnection</em> through proxy:<br/>
  (For facilitate I do not write down about ssl handle function for checking <em>serverTrusted</em> and <em>hostnameVerifier</em> issue.If need I will update.)<br/></p>
  
  <h1>1.Proxy class</h1>
  
  <pre class="lang-java prettyprint-override"><code>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("&lt;request&gt;test&lt;/request&gt;");
  owriter.flush();
  owriter.close();
  ...
  </code></pre>
  
  <p>This method workable and I observed packets flow also met my expectation.<br/>
  <em>HttpClient ---> ProxyServer ---> HttpServer</em><br/></p>
  
  <p>But when I use set Property method:<br/></p>
  
  <h1>2.setProperty</h1>
  
  <pre class="lang-java prettyprint-override"><code>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("&lt;request&gt;test&lt;/request&gt;");
  owriter.flush();
  owriter.close();
  ...
  </code></pre>
  
  <p>I got a <code>NoRouteToHostException: Network is unreachable</code>.<br/>
  It make me confused.I did not see any packets between HttpClient and ProxyServer.<br/>
  But HttpClient can ping to ProxyServer(10.100.12.10 ping 10.100.21.11)</p>
  
  <p>So I remove proxy setting(as without using proxy):<br/>
  Also got <code>NoRouteToHostException: Network is unreachable</code>.<br/>
  I thought this is reasonable.Because there is no route to extranet.<br/></p>
  
  <p>I guess it seems like to <em>setProperty</em> method that the inner function of <em>httpsUrlConnection</em> will to check this url can be reachable or not.<br/></p>
  
  <p>But it is weird. 1st method can be success.<br/></p>
  
  <p>Have any idea? Or what are different between 1st and 2nd method?<br/> </p>
  
  <p>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  <H1>Update</H1></p>
  
  <pre><code>System.setProperty("https.proxyHost",10.100.21.11);
  System.setProperty("https.proxyPort","80"); 
  </code></pre>
  
  <p>It can work and packets flow are correct what I expect for.<br/> 
  But set https.proxyPort=443 is not workable for me</p>
  
  <pre><code>System.setProperty("https.proxyPort","443");
  </code></pre>
  
  <p>It will thorow a exception as bellow:</p>
  
  <pre><code>java.net.SocketException: Unexpected end of file from server 
  at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:770)
  ....
  </code></pre>
  
  <p>So I thought Apache Proxy have also to be modified to the right configuration.</p>