HttpClient 实现 socks 代理
使用的环境
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.1</version> </dependency>
代码及 ConnectionSocketFactory 实现类
package xxx;import com.lucas.admin.util.HttpClientUtil;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.protocol.HttpClientContext;import org.apache.http.config.Registry;import org.apache.http.config.RegistryBuilder;import org.apache.http.conn.socket.ConnectionSocketFactory;import org.apache.http.conn.socket.PlainConnectionSocketFactory;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.protocol.HttpContext;import org.apache.http.ssl.SSLContexts;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.net.InetSocketAddress;import java.net.Proxy;import java.net.Socket;/** * @author kzcming * @since 2020/11/19 15:51 */public class Test { public static void main(String[] args) throws Exception { test("https://www.cnblogs.com/"); } public static void test(String url) throws Exception{ // ConnectionSocketFactory注册 Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", new MyConnectionSocketFactory()) .register("https",new MySSLConnectionSocketFactory()).build(); // HTTP客户端连接管理池 PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(reg); CloseableHttpClient httpclient = HttpClients.custom() .setConnectionManager(connManager) .build(); try { // socks代理地址 , socks 地址和端口,这里随便写了一个1008 InetSocketAddress socksaddr = new InetSocketAddress("你的地址", 1008); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); // 请求目标 HttpGet request = new HttpGet(url); System.out.println("----------------------------------------"); System.out.println("执行请求 :" + request.getRequestLine()); System.out.println("通过代理: " + socksaddr); System.out.println("----------------------------------------"); CloseableHttpResponse response = httpclient.execute(request, context); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println("返回响应:" + response.getStatusLine()); System.out.println("响应内容:" + EntityUtils.toString(entity)); System.out.println("----------------------------------------"); } finally { response.close(); } } finally { httpclient.close(); } } /** * 实现 http 链接的socket 工厂 */ static class MyConnectionSocketFactory extends PlainConnectionSocketFactory { @Override public Socket createSocket(final HttpContext context) throws IOException { InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address"); // socket代理 Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); return new Socket(proxy); } } /** * 实现 https 链接的socket 工厂 */ static class MySSLConnectionSocketFactory extends SSLConnectionSocketFactory { public MySSLConnectionSocketFactory() { super(SSLContexts.createDefault(), getDefaultHostnameVerifier()); } @Override public Socket createSocket(final HttpContext context) throws IOException { InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");// // socket代理 Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr); return new Socket(proxy); } }}
原文转载:http://www.shaoqun.com/a/492813.html
cicpa:https://www.ikjzd.com/w/1375
知无不言:https://www.ikjzd.com/w/1308
upc:https://www.ikjzd.com/w/111
HttpClient实现socks代理使用的环境<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.4.1</version></dependency>
首信易支付:首信易支付
moss:moss
玩转青城山&都江堰 - :玩转青城山&都江堰 -
个人做shopify—Facebook广告投放基础知识 (二):个人做shopify—Facebook广告投放基础知识 (二)
(图)甘孜扎坝:男不婚女不嫁 飞檐走壁的浪漫爱情 - :(图)甘孜扎坝:男不婚女不嫁 飞檐走壁的浪漫爱情 -
没有评论:
发表评论