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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析2. 教你如何寫出可維護(hù)的JS代碼3. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)6. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算7. Vue的Options用法說明8. css代碼優(yōu)化的12個(gè)技巧9. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
