不推荐使用HttpClient.getParams()。我该怎么用? [英] HttpClient.getParams() deprecated. What should I use instead?

查看:1737
本文介绍了不推荐使用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()客户端或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(); 

这篇关于不推荐使用HttpClient.getParams()。我该怎么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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