HttpClient.getParams() 已弃用.我应该用什么代替? [英] HttpClient.getParams() deprecated. What should I use instead?

查看:294
本文介绍了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");

我找不到一种方法从我的 request 对象读回参数(var1、var2)而不使用不推荐使用的方法,这应该很简单来自对象的属性.

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();

我认为您对来自客户端或 HttpMethod 的 getParams() 方法有点困惑,getParams() 不返回 URL 参数或类似的东西,返回客户端参数,如连接超时、代理、cookies...等

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 之后,您可以通过 RequestConfig<设置请求参数/code> 类使用 Builder

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());

更新:

如果您想从 HttpGetHttPost 请求对象中获取 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天全站免登陆