commons httpclient – 将查询string参数添加到GET / POST请求

我正在使用commons HttpClient对Spring servlet进行http调用。 我需要在查询string中添加一些参数。 所以我做了以下几点:

HttpRequestBase request = new HttpGet(url); HttpParams params = new BasicHttpParams(); params.setParameter("key1", "value1"); params.setParameter("key2", "value2"); params.setParameter("key3", "value3"); request.setParams(params); HttpClient httpClient = new DefaultHttpClient(); httpClient.execute(request); 

但是当我尝试读取servlet中的参数使用

 ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getParameter("key"); 

它返回null。 实际上parameterMap是完全空的。 在创buildHttpGet请求之前手动将参数附加到url时,参数在servlet中可用。 同样,当我从浏览器使用带有queryString的URL来追加servlet。

这里有什么错误? 在httpclient 3.x中,GetMethod有一个setQueryString()方法来追加查询string。 4.x中的等价物是什么?

以下是一些示例代码,您将如何使用4.x http客户端添加查询string参数

  URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost(host).setPort(port).setPath(restPath + taskUri + "/" + taskId) .setParameter("parts", "all") .setParameter("params", routingOptionsJson) .setParameter("action", "finish"); HttpPost post = getHttpPostMethod(builder.build()); 

得到的URI看起来像http:/ www … com / parts = all&params =%23stuff%23&action = finish

希望这可以帮助。

如果您希望在创build请求后添加查询参数,请尝试将HttpRequest强制转换为HttpBaseRequest 。 然后,您可以更改已铸造请求的URI:

 HttpGet someHttpGet = new HttpGet("http://google.de"); URI uri = new URIBuilder(someHttpGet.getURI()).addParameter("q", "That was easy!").build(); ((HttpRequestBase) someHttpGet).setURI(uri); 

HttpParams接口不用于指定查询string参数,它用于指定HttpClient对象的运行时行为。

如果你想传递查询string参数,你需要自己组装它们,例如

 new HttpGet(url + "key1=" + value1 + ...); 

请记住先编码值(使用URLEncoder )。

 I am using httpclient 4.4. For solr query I used the following way and it worked. NameValuePair nv2 = new BasicNameValuePair("fq","(active:true) AND (category:Fruit OR category1:Vegetable)"); nvPairList.add(nv2); NameValuePair nv3 = new BasicNameValuePair("wt","json"); nvPairList.add(nv3); NameValuePair nv4 = new BasicNameValuePair("start","0"); nvPairList.add(nv4); NameValuePair nv5 = new BasicNameValuePair("rows","10"); nvPairList.add(nv5); HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); URI uri = new URIBuilder(request.getURI()).addParameters(nvPairList).build(); request.setURI(uri); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() != 200) { } BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output .... "); String respStr = ""; while ((output = br.readLine()) != null) { respStr = respStr + output; System.out.println(output); } 

这种方法是可以的,但是当你dynamic地得到参数,有时候是1,2,3或更多,就像SOLRsearch查询(例如)

这是一个更灵活的解决scheme。 原油,但可以提炼。

 public static void main(String[] args) { String host = "localhost"; String port = "9093"; String param = "/10-2014.01?description=cars&verbose=true&hl=true&hl.simple.pre=<b>&hl.simple.post=</b>"; String[] wholeString = param.split("\\?"); String theQueryString = wholeString.length > 1 ? wholeString[1] : ""; String SolrUrl = "http://" + host + ":" + port + "/mypublish-services/carclassifications/" + "loc"; GetMethod method = new GetMethod(SolrUrl ); if (theQueryString.equalsIgnoreCase("")) { method.setQueryString(new NameValuePair[]{ }); } else { String[] paramKeyValuesArray = theQueryString.split("&"); List<String> list = Arrays.asList(paramKeyValuesArray); List<NameValuePair> nvPairList = new ArrayList<NameValuePair>(); for (String s : list) { String[] nvPair = s.split("="); String theKey = nvPair[0]; String theValue = nvPair[1]; NameValuePair nameValuePair = new NameValuePair(theKey, theValue); nvPairList.add(nameValuePair); } NameValuePair[] nvPairArray = new NameValuePair[nvPairList.size()]; nvPairList.toArray(nvPairArray); method.setQueryString(nvPairArray); // Encoding is taken care of here by setQueryString } }