如何用 RequestConfig 替换已弃用的 httpClient.getParams()? [英] How do I replace Deprecated httpClient.getParams() with RequestConfig?

查看:46
本文介绍了如何用 RequestConfig 替换已弃用的 httpClient.getParams()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了代码

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 并读取

httpClient.getParams() is @Deprecated and reads "

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

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

There are no class docs for RequestConfig and I do not know what method should be used to replace getParams() and ConnRoutePNames.DEFAULT_PROXY.

推荐答案

您正在使用 apache HttpClient 4.3 库和 apache HttpClient 4.2 代码.

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

请注意,getParams() 和 ConnRoutePNames 并不是您案例中唯一不推荐使用的方法.DefaultHttpClient 类本身依赖于 4.2 实现,并且在 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.

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

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:

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

这篇关于如何用 RequestConfig 替换已弃用的 httpClient.getParams()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆