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   util   

HttpClient 瀹炵幇 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悊的主要内容,如果未能解决你的问题,请参考以下文章

寰俊app鏀粯python浠g爜瀹炵幇

娣卞害瀛︿範鍘熺悊璇﹁В鍙奝ython浠g爜瀹炵幇

Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯

瀹炵幇涓€涓唬鐮佺紪璇戝櫒锛?00琛孞S浠g爜锛?/h1>

property璇﹁В

鏀寔鍚戦噺鏈猴細Stata 鍜?Python 瀹炵幇