Java HttpClient實(shí)現(xiàn)socks代理的示例代碼
HttpClient 實(shí)現(xiàn) socks 代理
使用的環(huán)境
<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 實(shí)現(xiàn)類
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注冊(cè) 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 地址和端口,這里隨便寫了一個(gè)1008 InetSocketAddress socksaddr = new InetSocketAddress("你的地址", 1008); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); // 請(qǐng)求目標(biāo) HttpGet request = new HttpGet(url); System.out.println("----------------------------------------"); System.out.println("執(zhí)行請(qǐng)求 :" + 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("返回響應(yīng):" + response.getStatusLine()); System.out.println("響應(yīng)內(nèi)容:" + EntityUtils.toString(entity)); System.out.println("----------------------------------------"); } finally { response.close(); } } finally { httpclient.close(); } } /** * 實(shí)現(xiàn) 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); } } /** * 實(shí)現(xiàn) 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); } } }
以上就是Java HttpClient 實(shí)現(xiàn) socks 代理的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java HttpClient 實(shí)現(xiàn) socks 代理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java多線程之鎖學(xué)習(xí)(增強(qiáng)版)
這篇文章主要為大家詳細(xì)介紹了Java多線程中鎖的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們了解線程有一定幫助,需要的可以參考一下2023-02-02Java excel數(shù)據(jù)導(dǎo)入mysql的實(shí)現(xiàn)示例詳解
今天教大家如何使用Java將excel數(shù)據(jù)導(dǎo)入MySQL,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴呢很有幫助,需要的朋友可以參考下2022-08-08詳解Spring Boot中使用AOP統(tǒng)一處理Web請(qǐng)求日志
本篇文章主要介紹了詳解Spring Boot中使用AOP統(tǒng)一處理Web請(qǐng)求日志,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Spring對(duì)靜態(tài)變量無法注入的解決方案
這篇文章主要介紹了使用Spring對(duì)靜態(tài)變量無法注入的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-07-07LambdaQueryWrapper與QueryWrapper的使用方式
這篇文章主要介紹了LambdaQueryWrapper與QueryWrapper的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05如何在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串
這篇文章主要介紹了在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串類型數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06詳解Java編程規(guī)約(命名風(fēng)格、常量定義、代碼格式)
這篇文章主要介紹了詳解Java編程規(guī)約(命名風(fēng)格、常量定義、代碼格式),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-10-10