如何用RequestConfig替换不推荐使用的httpClient.getParams()? [英] How do I replace Deprecated httpClient.getParams() with RequestConfig?
问题描述
我继承了代码
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.2代码的apache HttpClient 4.3库。
You are using apache HttpClient 4.3 library with apache HttpClient 4.2 code.
请注意,getParams()和ConnRoutePNames不是唯一不推荐使用的你的情况下的方法.NaultHttpClient类本身依赖于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/教程/ HTML / connmgmt.html#d5e4 73 ),你可以这样重写:
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屋!