Apache HTTP客户端,POST请求.如何正确设置请求参数?

编程入门 行业动态 更新时间:2024-10-15 02:25:30
本文介绍了Apache HTTP客户端,POST请求.如何正确设置请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Apache HTTP Client,需要将POST请求发送到我的servlet. 发送请求时,我的servlet没有收到任何参数(在HttpServletRequest中).

I'm using Apache HTTP Client and I need to send a POST request to my servlet. When the request is sent my servlet does not receive any parameters (in the HttpServletRequest).

这是客户端程序代码:

// Engage the HTTP client DefaultHttpClient httpclient = new DefaultHttpClient(); HttpResponse response; try { HttpPost httpPost = new HttpPost("localhost:8080/test-json-web/JSONReceiverServlet"); // Setup the request parameters HttpParams params = new BasicHttpParams(); params.setParameter("taskdef", task1JsonString); httpPost.setParams(params); // Make the request response = httpclient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if(responseEntity != null) { System.out.println("Response content length: " + responseEntity.getContentLength()); } String jsonResultString = EntityUtils.toString(responseEntity); EntityUtils.consume(responseEntity); System.out.println("----------------------------------------"); System.out.println("result:"); System.out.println(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); }

如何正确设置POST请求参数,以便servlet实际接收它们?

How to correctly set POST request parameters so that servlet actually receives them?

推荐答案

尝试一下:

List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("IDToken1", "username")); nvps.add(new BasicNameValuePair("IDToken2", "password")); httPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

更多推荐

Apache HTTP客户端,POST请求.如何正确设置请求参数?

本文发布于:2023-11-25 04:55:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何正确   客户端   参数   Apache   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!