Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have inherited the code

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



private HttpClient createHttpClientOrProxy() {
    HttpClient httpclient = new DefaultHttpClient();

    /*
     * Set an HTTP proxy if it is specified in system properties.
     * 
     * http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
     * http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java
     */
    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");
// @Deprecated methods here... getParams() and ConnRoutePNames
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }
    return httpclient;
}

httpClient.getParams() is @Deprecated and reads "

HttpParams  getParams()
Deprecated. 
(4.3) use RequestConfig.

There are no class docs for RequestConfig and I do not know what method should be used to replace getParams() and ConnRoutePNames.DEFAULT_PROXY.

like image 923
peter.murray.rust Avatar asked Dec 21 '13 08:12

peter.murray.rust


People also ask

Is Apache HttpClient deprecated?

From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated. Use following maven dependency as an example.

What is closable HttpClient?

CloseableHttpClient is an abstract class which is the base implementation of HttpClient that also implements java. io. Closeable.

Do we need to close HttpClient connection?

If you are processing HTTP responses manually instead of using a response handler, you need to close all the http connections by yourself.


1 Answers

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();
like image 197
ok2c Avatar answered Oct 06 '22 00:10

ok2c