HttpClient:如何从现有连接获取底层套接字?

我正在使用HttpClient 4.02通过代理创建连接(使用CONNECT方法)来连接到远程服务器的连接。 HttpClient非常方便,但我是API的新手,无法看到如何获取隧道连接的底层Socket

以下代码取自: http : //svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0.1/httpclient/src/examples/org/apache/http/examples/client/ClientExecuteProxy.java

  // 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(); 

这很好地建立了连接,但有没有办法获得底层的Socket ,以便我使用自定义协议与target.server.net上的服务器通信?

在项目的JIRA中打开更改请求。 这个function很容易被忽视。 虽然从3.x组合一个等效的ProxyClient应该是相当简单的,但是使用HttpClient的库存版本运送一个是有意义的。

编辑:

自版本4.2起可用。 请参阅http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/impl/client/ProxyClient.html

不确定我是否完全理解你的要求,但我会尽我所能……

试试这个: http : //svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/ProxyTunnelDemo.java?view=markup

在评论中,@ willjcroz说:

这样我的应用程序就可以用于防火墙后面的人,唯一的出路就是Web代理。 通过连接到代理,它将数据作为HTTPS转发到目标服务器,从而提供与目标服务器的安全连接。

如果要通过Web代理,协议感知防火墙等成功隧道传输协议,则应用程序的客户端和服务器端必须符合HTTP协议规范。 如果您(以某种方式)设法深入到套接字级别,则可能违反协议…

相反,您应该通过将数据放入HTTP请求和响应的内容,使用消息/响应中的自定义HTTP标头,自定义内容类型等来实现自定义协议。