title: Accessing HTTPS web services through proxy tunnel using org.apache.http.impl.client.ProxyClient
body: <p>I need to access some https soap services through a http proxy tunnel. So I am using org.apache.http.impl.client.ProxyClient to connect to the target host through proxy tunnel. Which returns me a socket connected to the target host through the proxy server. The returned socket is connected properly to the target system. Now I need to invoke soap services hosted in the target system. 
  But I am clueless how can I access  those https services through socket. Below is my sample program. 
  Sample url for service that needs to be invoked 
  <a href="https://XX.XX.XX.XX:44330/sampleService/1.0" rel="nofollow noreferrer">https://XX.XX.XX.XX:44330/sampleService/1.0</a></p>
  
  <pre><code>public static void proxyTunnelDemo(String url,String soapRequestBody) throws IOException, HttpException {
      ProxyClient proxyClient = new ProxyClient();
      HttpHost target = new HttpHost("XX.XX.XX.XX", 44330);
      HttpHost proxy = new HttpHost("YY.YY.YY.YY", 9293);
      UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("********", "********");
      Socket socket = proxyClient.tunnel(proxy, target, credentials);
  
      // Need to access Web service through socket .
      // request method GET 
      // PORT  = 44330
      // url = https://XX.XX.XX.XX:44330/sampleService/1
      //soapRequestBody =  soap message needs to be sent.
  String SOAP_CREDENTIALS = "*********";
  SSLSocketFactory factory =  (SSLSocketFactory)SSLSocketFactory.getDefault();
      SSLSocket sslSocket = (SSLSocket)factory.createSocket(socket, target.getHostName(), target.getPort(), true);
      sslSocket.startHandshake();
  
  try {
  
  
          //Send header
          BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), StandardCharsets.UTF_8));
          String authorization  =  new BASE64Encoder().encode(SOAP_CREDENTIALS.getBytes());
          // You can use "UTF8" for compatibility with the Microsoft virtual machine.
  
          wr.write("GET / HTTP/1.1\r\n");
          wr.write("Host: "+target.getHostName()+"\r\n");
          wr.write("Content-Length:"+ soapRequestBody.length() + "\r\n");
          wr.write("Content-Type: text/html\"\r\n");
          wr.write("SOAPAction:"+url+"\r\n");
          wr.write("Authorization: Basic "+authorization+"\r\n");
          wr.write("\r\n ");           //Send data
          wr.write(soapRequestBody);
          wr.flush();
  
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(sslSocket.getInputStream(), StandardCharsets.UTF_8));
          String line = null;
          while ((line = in.readLine()) != null) {
              System.out.println(line);
          }
  
      } finally {
          socket.close();
          sslSocket.close();
      }
  }
  </code></pre>
  
  <p>With the above code getting error response below<br>
  HTTP/1.1 400 Error
  X-Backside-Transport: FAIL FAIL
  Content-Type: text/xml
  Connection: close</p>