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 PoolingHttpClientConnecti.........