Java HttpClient.getParams() 已弃用。我应该用什么代替?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22038957/
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
HttpClient.getParams() deprecated. What should I use instead?
提问by donnadulcinea
I am using apache-httpclient-4.3. I would analyze a http request, in particular the query string parameters, but
我正在使用 apache-httpclient-4.3。我会分析一个 http 请求,特别是查询字符串参数,但是
@Deprecated
public HttpParams getParams()
Deprecated. (4.3) use constructor parameters of configuration API provided by HttpClient
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:
我不确定这意味着什么。我应该使用一些配置 API 的构造函数参数(那是什么?HostConfiguration 不再作为类可用)。但是在构建阶段我直接通过url传递查询参数:
HttpGet request = new HttpGet("http://example.com/?var1=value1&var2=value2");
I can't find a way to read back the parameters (var1, var2) from my requestobject without using deprecated methods, which should be simple as to get attributes from an object.
如果不使用不推荐使用的方法,我找不到从我的请求对象中读回参数(var1, var2)的方法,这应该很简单,因为从对象获取属性。
采纳答案by vzamanillo
You can use an URIBuilderobject
您可以使用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();
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
我认为您对getParams()
来自客户端或 HttpMethod的方法有点困惑,getParams()
不返回 URL 参数或类似的东西,返回客户端参数,如连接超时、代理、cookie...等
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
在 4.3.2 之前,您可以使用getParams()
方法(现已弃用)为客户端设置参数,在 4.3.2 之后,您可以RequestConfig
使用类通过类设置请求参数Builder
Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectionRequestTimeout(1000).setMaxRedirects(1);
and then set to the HttpMethod
only (not to client like before)
然后设置为HttpMethod
唯一(不像以前那样设置为客户端)
request.setConfig(requestConfigBuilder.build());
Update:
更新:
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
或HttPost
请求对象中获取 URI 参数,你可以URIBuilder
以同样的方式使用
HttpGet request = new HttpGet("http://example.com/?var=1&var=2");
URIBuilder newBuilder = new URIBuilder(request.getURI());
List<NameValuePair> params = newBuilder.getQueryParams();