Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpClient: how do I obtain the underlying socket from an existing connection?
    text
    copied!<p>I am using HttpClient 4.02 to create a connection via proxy (using the CONNECT method) to tunnel a connection to a remote server. HttpClient is very convenient for this but I am new to the API and cannot see how to get at the underlying <code>Socket</code> of the tunneled connection.</p> <p>The following code taken from: <a href="http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0.1/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java" rel="nofollow noreferrer">http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0.1/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java</a></p> <pre><code> // make sure to use a proxy that supports CONNECT HttpHost target = new HttpHost("target.server.net", 443, "https"); HttpHost proxy = new HttpHost("some.proxy.net", 8080, "http"); // general setup SchemeRegistry supportedSchemes = new SchemeRegistry(); // Register the "http" and "https" protocol schemes, they are // required by the default operator to look up socket factories. supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); // prepare parameters HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, supportedSchemes); DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target + " via " + proxy); HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); </code></pre> <p>This sets up the connection nicely but is there a way to get at the underlying <code>Socket</code> in order for me to use a custom protocol to talk to the server at target.server.net?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload