gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:10 25 4
gpt4 key购买 nike

我正在使用 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,以便我使用自定义协议(protocol)与 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

关于java - HttpClient:如何从现有连接获取底层套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3741417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com