org httpclientbuilder example ejemplo java apache http

java - httpclientbuilder - httpClient.getConnectionManager() está en desuso. ¿Qué debería usarse en su lugar?



import org apache http httpentity maven (1)

He heredado el codigo

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() lee "

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

Los documentos para HttpClientBuilder parecen escasos y simplemente dicen:

Builder for CloseableHttpClient instances.

Sin embargo, si sustituyo HttpClient por CloseableHttpClient el método todavía parece @Deprecated .

¿Cómo puedo usar un método no en desuso?


En lugar de crear una nueva instancia de HttpClient, use el Generador. Usted obtendría un CloseableHttpClient.

por ejemplo, uso:

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

En lugar de usar getConnectionManager (). Shutdown (), use el método close () en lugar de CloseableHttpClient.