问题描述
我正在使用HttpClient 4.02通过代理创建连接(使用CONNECT方法)来连接到远程服务器的连接。 HttpClient非常方便,但我是API的新手,无法看到如何获取隧道连接的底层 Socket
。
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 Socket
of the tunneled connection.
以下代码取自:
// 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();
这很好地建立了连接,但有没有办法获得基础套接字
为了让我使用自定义协议与target.server.net上的服务器通信?
This sets up the connection nicely but is there a way to get at the underlying Socket
in order for me to use a custom protocol to talk to the server at target.server.net?
推荐答案
在项目的JIRA中打开更改请求。这个功能很容易被忽视。虽然从3.x组合相当于ProxyClient应该是相当微不足道的,但是使用HttpClient的库存版本运送它是有意义的。
Open a change request in the project's JIRA. This feature simply got overlooked. While it should be fairly trivial to put together an equivalent of ProxyClient from 3.x, it makes sense to ship one with the stock version of HttpClient.
编辑:
自版本4.2起可用。请参阅
Available since version 4.2. See http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/impl/client/ProxyClient.html
这篇关于HttpClient:如何从现有连接获取底层套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!