java-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中打开更改请求.此功能只是被忽略了.虽然将3.x中的等效ProxyClient放在一起相当琐碎,但将其与HttpClient的普通版本一起发货是有意义的.

编辑:

自4.2版起可用.查看http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/impl/client/ProxyClient.html

上一篇:linux-通过ssh隧道路由RDP


下一篇:Vue使用过滤器Vue.filter()对data中的数据进行处理