本文介绍了httpClient.getConnectionManager()被德precated - 应该怎样来代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了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()

@Deprecated
ClientConnectionManager getConnectionManager()

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

有关HttpClientBuilder的文档似乎稀疏,简单地说:

The docs for HttpClientBuilder seem sparse and simply say:

Builder for CloseableHttpClient instances.

但是,如果我取代的HttpClient CloseableHttpClient 的方法似乎仍 @德precated

我如何使用非德precated方法?

How can I use a non-Deprecated method?

推荐答案

而不是创建HttpClient的一个新的实例,使用Builder。你会得到一个CloseableHttpClient。

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

例如用法:

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

而不是使用getConnectionManager的()。shutdown()方法,使用close()方法,而不是在CloseableHttpClient。

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

这篇关于httpClient.getConnectionManager()被德precated - 应该怎样来代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 22:42