我正在使用 HttpClient 4.02 通过代理创建连接(使用 CONNECT 方法)以隧道连接到远程服务器。HttpClient 对此非常方便,但我是 API 新手,看不到如何获取Socket
隧道连接的底层。
// 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 上的服务器通信?