• HttpClient 链接管理


    SOCK is a TCP/IP level proxy protocol, not HTTP. It is not supported by HttpClient out of the box.

    One can customize HttpClient to establish connections via a SOCKS proxy by using a custom connection socket factory

    EDIT: changes to SSL instead of plain sockets

    Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", new MyConnectionSocketFactory(SSLContexts.createSystemDefault()))
            .build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
    CloseableHttpClient httpclient = HttpClients.custom()
            .setConnectionManager(cm)
            .build();
    try {
        InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
        HttpClientContext context = HttpClientContext.create();
        context.setAttribute("socks.address", socksaddr);
    
        HttpHost target = new HttpHost("localhost", 80, "http");
        HttpGet request = new HttpGet("/");
    
        System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr);
        CloseableHttpResponse response = httpclient.execute(target, request, context);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }

    1

    static class MyConnectionSocketFactory extends SSLConnectionSocketFactory {
    
        public MyConnectionSocketFactory(final SSLContext sslContext) {
            super(sslContext);
        }
    
        @Override
        public Socket createSocket(final HttpContext context) throws IOException {
            InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
            Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
            return new Socket(proxy);
        }
    
    }

    1. doc

    2.翻译

  • 相关阅读:
    CSS布局之盒子模型[二]
    CSS布局之盒子模型[一]
    CSS文本相关之垂直排列[5]
    网站迁移之后,中文路径都变成乱码
    Linux中shell搜索多文件中的字符串
    mysql数据库报错
    使用Flarum轻松搭建自己的论坛
    CSS雪碧图-html优化
    CSS-定位模式
    ul当做div标签的使用
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7772554.html
Copyright © 2020-2023  润新知