HttpClient 瀹炵幇 socks 浠g悊
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient 瀹炵幇 socks 浠g悊相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/cto' title='cto'>cto
ddr dmi custom manager reg logs throws utilHttpClient 瀹炵幇 socks 浠g悊
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浠g悊鍦板潃 , socks 鍦板潃鍜岀鍙?杩欓噷闅忎究鍐欎簡涓€涓?008
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("閫氳繃浠g悊锛?" + 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 閾炬帴鐨剆ocket 宸ュ巶
*/
static class MyConnectionSocketFactory extends PlainConnectionSocketFactory {
@Override
public Socket createSocket(final HttpContext context) throws IOException {
InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
// socket浠g悊
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
}
}
/**
* 瀹炵幇 https 閾炬帴鐨剆ocket 宸ュ巶
*/
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浠g悊
Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
return new Socket(proxy);
}
}
}
以上是关于HttpClient 瀹炵幇 socks 浠g悊的主要内容,如果未能解决你的问题,请参考以下文章