Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httpClient.getConnectionManager() is deprecated - what should be used instead?

Tags:

java

http

apache

I have inherited the code

import org.apache.http.client.HttpClient; ... HttpClient httpclient = createHttpClientOrProxy();      try {         HttpPost postRequest = postRequest(data, url);         body = readResponseIntoBody(body, httpclient, postRequest);     } catch( IOException ioe ) {         throw new RuntimeException("Cannot post/read", ioe);     } finally {         httpclient.getConnectionManager().shutdown();  // ** Deprecated     }   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")) ) {         log.warn("http.proxyHost = " + System.getProperty("http.proxyHost") );         log.warn("http.proxyPort = " + System.getProperty("http.proxyPort"));         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; } 

getConnectionManager() reads "

@Deprecated ClientConnectionManager getConnectionManager()  Deprecated. (4.3) use HttpClientBuilder. Obtains the connection manager used by this client. 

The docs for HttpClientBuilder seem sparse and simply say:

Builder for CloseableHttpClient instances. 

However if I replace HttpClient with CloseableHttpClient the method still seems @Deprecated.

How can I use a non-Deprecated method?

like image 717
peter.murray.rust Avatar asked Dec 20 '13 23:12

peter.murray.rust


People also ask

Is Apache HttpClient deprecated?

From Apache HTTP Client API version 4.3 on wards, DefaultHttpClient is deprecated.

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.

Is Java HttpClient thread safe?

HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager.

What is Apache HttpClient used for?

Http client is a transfer library. It resides on the client side, sends and receives Http messages. It provides up to date, feature-rich, and an efficient implementation which meets the recent Http standards.


1 Answers

Instead of creating a new instance of HttpClient, use the Builder. You would get a CloseableHttpClient.

e.g usage:

CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(proxy).build() 

Instead of using getConnectionManager().shutdown(), use the close() method instead on CloseableHttpClient.

like image 148
SiN Avatar answered Sep 18 '22 14:09

SiN