亲宝软件园·资讯

展开

Java HttpClient实现socks代理的代码实例

kzcming 人气:0

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);
    }
  }


}

加载全部内容

相关教程
猜你喜欢
用户评论