ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-05 21:12:34  阅读:291  来源: 互联网

标签:sockets proxy httpclient tunnel java


我正在使用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

标签:sockets,proxy,httpclient,tunnel,java
来源: https://codeday.me/bug/20191105/1997844.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有