zoukankan      html  css  js  c++  java
  • HttpClient(4.3.5)

    Even though HttpClient is aware of complex routing scemes and proxy chaining, it supports only simple direct or one hop proxy connections out of the box.

    The simplest way to tell HttpClient to connect to the target host via a proxy is by setting the default proxy parameter:

    HttpHost proxy = new HttpHost("someproxy", 8080);
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRoutePlanner(routePlanner)
            .build();

    One can also instruct HttpClient to use the standard JRE proxy selector to obtain proxy information:

    SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRoutePlanner(routePlanner)
            .build();

    Alternatively, one can provide a custom RoutePlanner implementation in order to have a complete control over the process of HTTP route computation:

    HttpRoutePlanner routePlanner = new HttpRoutePlanner() {
    
        public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
            return new HttpRoute(target, null,  new HttpHost("someproxy", 8080),
                    "https".equalsIgnoreCase(target.getSchemeName()));
        }
    
    };
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRoutePlanner(routePlanner)
            .build();
        }
    }

    如果想针对不同的请求设置不同的代理,可以通过 RequestConfig 设置代理,然后在执行请求时带上 RequestConfig 参数。

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpHost proxy = new HttpHost("someproxy", 8080);
    RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    
    HttpGet httpGet = new HttpGet("http://example.com");        
    httpGet.setConfig(config);
    CloseableHttpResponse response = httpClient.execute(httpGet);
  • 相关阅读:
    漫步温泉大道有感
    不可多得的”魔戒“:一堂成功学大师们的浓缩课
    四川新闻网关于IT诗人的报道
    赠徐蕴筝(帮别人名字作诗)
    再游草堂
    赠申芳菲(帮别人名字作诗)
    Oracle内部错误:ORA00600[15801], [1]一例
    Oracle内部错误:ORA00600[OSDEP_INTERNAL]一例
    Oracle O立方服务平台(O3SP)
    Oracle RAC内部错误:ORA00600[keltnfyldmInit]一例
  • 原文地址:https://www.cnblogs.com/huey/p/5721626.html
Copyright © 2011-2022 走看看