【问题标题】:How do I replace Deprecated httpClient.getParams() with RequestConfig?如何用 RequestConfig 替换已弃用的 httpClient.getParams()?
【发布时间】:2014-01-10 02:24:04
【问题描述】:

我继承了代码

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() 被@Deprecated 并读作“

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

RequestConfig 没有类文档,我不知道应该用什么方法替换getParams()ConnRoutePNames.DEFAULT_PROXY

【问题讨论】:

    标签: java proxy apache-httpclient-4.x


    【解决方案1】:

    您正在使用带有 apache HttpClient 4.2 代码的 apache HttpClient 4.3 库。

    请注意 getParams() 和 ConnRoutePNames 不是您案例中唯一不推荐使用的方法。 DefaultHttpClient 类本身依赖于 4.2 的实现,在 4.3 中也已弃用。

    关于这里的4.3文档(http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e473),你可以这样重写:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      这更像是@Stephane Lallemagne 给出的答案的后续

      有一种更简洁的方法可以让 HttpClient 获取系统代理设置

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

      或者如果你想要一个使用系统默认值完全配置的 HttpClient 实例

      CloseableHttpClient client = HttpClients.createSystem();
      

      【讨论】:

      • 谢谢;奥列格我只是重复使用你的答案here
      猜你喜欢
      • 1970-01-01
      • 2020-02-27
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多