How do I replace Deprecated httpClient.getParams() with RequestConfig?

后端 未结 2 1635
傲寒
傲寒 2020-12-31 03:23

I have inherited the code

import org.apache.http.client.HttpClient;
...
HttpClient httpclient = createHttpClientOrProxy();
...



private HttpClient createH         


        
相关标签:
2条回答
  • 2020-12-31 03:43

    This is more of a follow-up to the answer given by @Stephane Lallemagne

    There is a much conciser way of making HttpClient pick up system proxy settings

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

    or this if you want an instance of HttpClient fully configured with system defaults

    CloseableHttpClient client = HttpClients.createSystem();
    
    0 讨论(0)
  • 2020-12-31 03:56

    You are using apache HttpClient 4.3 library with apache HttpClient 4.2 code.

    Please notice that getParams() and ConnRoutePNames are not the only deprecated methods in your case. The DefaultHttpClient class itself rely on 4.2 implementation and is also deprecated in 4.3.

    In regard to the 4.3 documentation here (http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e473), you can rewrite it this way:

    private HttpClient createHttpClientOrProxy() {
    
        HttpClientBuilder hcBuilder = HttpClients.custom();
    
        // Set HTTP proxy, if specified in system properties
        if( isSet(System.getProperty("http.proxyHost")) ) {
            int port = 80;
            if( isSet(System.getProperty("http.proxyPort")) ) {
                port = Integer.parseInt(System.getProperty("http.proxyPort"));
            }
            HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"), port, "http");
            DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
            hcBuilder.setRoutePlanner(routePlanner);
        }
    
        CloseableHttpClient httpClient = hcBuilder.build();
    
        return httpClient;
    }
    
    0 讨论(0)
提交回复
热议问题