不推荐使用HttpClient.getParams()。我该怎么用?

问题描述:

我正在使用apache-httpclient-4.3。我会分析一个http请求,特别是查询字符串参数,但是

I am using apache-httpclient-4.3. I would analyze a http request, in particular the query string parameters, but

@Deprecated
public HttpParams getParams()
Deprecated. (4.3) use constructor parameters of configuration API provided by HttpClient

我不知道这意味着什么。我应该使用一些配置API的构造函数参数(那是什么?HostConfiguration不再作为类提供)。但是在构建阶段我直接通过url传递查询参数:

I am not sure to understand what this means. I should use the constructor parameters of some configuration API (what's that? HostConfiguration is no more available as class). But during the construction phase I directly pass the query parameters through the url:

HttpGet request = new HttpGet("http://example.com/?var1=value1&var2=value2");

我找不到回读参数的方法( var1,var2 )来自我的 request 对象而不使用弃用的方法,这应该很简单,以便从对象获取属性。

I can't find a way to read back the parameters (var1, var2) from my request object without using deprecated methods, which should be simple as to get attributes from an object.

您可以使用 URIBuilder 对象

URIBuilder builder = new URIBuilder("http://example.com/");
builder.setParameter("var1", "value1").setParameter("var2", "value2");

HttpGet request = new HttpGet(builder.build());

// get back the url parameters   
List<NameValuePair> params = builder.getQueryParams();

我认为你对 getParams()$感到有点困惑c $ c>客户端或HttpMethod的方法, getParams()不返回URL参数或类似的东西,返回客户端参数,如连接超时,代理,cookie。 ..等等

I think you are a bit confused about the getParams() method from the client or HttpMethod, getParams() does not return the URL parameters or something like that, returns the client parameteres like connection timeout, proxy, cookies... etc

在4.3.2之前,您可以使用 getParams()方法将参数设置到客户端(现在已弃用),在4.3.2之后,您可以使用 Builder

通过 RequestConfig 类设置请求参数>

Before 4.3.2 you could set the parameters to the client using the getParams() method (deprecated now), after 4.3.2 you can set the request params via the RequestConfig class using a Builder

Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectionRequestTimeout(1000).setMaxRedirects(1);

然后设置为 HttpMethod (不像以前那样对客户来说)

and then set to the HttpMethod only (not to client like before)

request.setConfig(requestConfigBuilder.build());

更新:

如果要从 HttpGet HttPost 请求对象获取URI参数,可以使用 URIBuilder 以同样的方式

If you want to get the URI parameters from an HttpGet or HttPost request object you can use the URIBuilder in the same way

HttpGet request = new HttpGet("http://example.com/?var=1&var=2");

URIBuilder newBuilder = new URIBuilder(request.getURI());
List<NameValuePair> params = newBuilder.getQueryParams();