HttpClient.getParams() deprecated. What should I use instead?

…衆ロ難τιáo~ 提交于 2019-11-27 02:30:59

问题


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

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

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.


回答1:


You can use an URIBuilder object

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

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

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

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 request = new HttpGet("http://example.com/?var=1&var=2");

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


来源:https://stackoverflow.com/questions/22038957/httpclient-getparams-deprecated-what-should-i-use-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!