Java 如何用 RequestConfig 替换已弃用的 httpClient.getParams()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20716909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:47:05  来源:igfitidea点击:

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

javaproxyapache-httpclient-4.x

提问by peter.murray.rust

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 "

httpClient.getParams()是@Deprecated 并读取“

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.

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

采纳答案by Stephane Lallemagne

You are using apache HttpClient 4.3 library with apache HttpClient 4.2 code.

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

Please notice that getParams() and ConnRoutePNames are not the only deprecated methods in your case. The DefaultHttpClient class itself rely on 4.2 implementation and is also deprecated in 4.3.

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

In regard to the 4.3 documentation here (http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e473), you can rewrite it this way:

关于此处的 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;
}

回答by ok2c

This is more of a follow-up to the answer given by @Stephane Lallemagne

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

There is a much conciser way of making HttpClient pick up system proxy settings

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

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

or this if you want an instance of HttpClient fully configured with system defaults

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

CloseableHttpClient client = HttpClients.createSystem();