httpclient put请求发送参数,服务端无法接收到

Be Patient K 2017-06-06 09:45:59
httpclient端代码:
public static void main(String[] args) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
UrlEncodedFormEntity entity = null;
try {

HttpPut put = new HttpPut("http://192.168.1.125:8080/hpclientTest/putest");
put.setHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
put.setHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
List<NameValuePair>list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("id", "11"));
list.add(new BasicNameValuePair("nd","2017"));
list.add(new BasicNameValuePair("name","傻B"));

entity = new UrlEncodedFormEntity(list,"UTF-8");
put.setEntity(entity);
CloseableHttpResponse response = client.execute(put);
HttpEntity et = response.getEntity();
InputStream in = et.getContent();
int stat= response.getStatusLine().getStatusCode();

} catch (Exception e) {

e.printStackTrace();
}finally{
client.close();
}
}

服务端代码:
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String id = req.getParameter("id");
String nd = req.getParameter("nd");
String name = req.getParameter("name");
System.out.println(id+nd+":"+name);
//System.out.println(req.getInputStream());
resp.getWriter().print("{\"reulst\":\"0\"}");

}
不管是用put方法还是service方法接收参数,参数都是为null 怎么就解决?
...全文
715 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Be Patient K 2017-06-09
  • 打赏
  • 举报
回复
引用 1 楼 pany1209 的回复:

HttpClient代码:
CloseableHttpClient httpClient=HttpClients.createDefault();
			CloseableHttpResponse response=null;
			HttpPut put=new HttpPut("url访问");
			put.setHeader("Content-type", "application/json");
			Info info=new Info("1", "2017", "傻逼");
			ObjectMapper mapper=new ObjectMapper();
			String params=null;
			try {
				params = mapper.writeValueAsString(info);
				StringEntity stringEntity=new StringEntity(params, "UTF-8");
				put.setEntity(stringEntity);
				response=httpClient.execute(put);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(httpClient!=null){
					try {
						httpClient.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(response!=null){
					try {
						response.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
Controller代码:

@RequestMapping("/路径")
	public String testUrl(@RequestBody Info info){
		System.out.println("name="+info.getName());
		return null;
	}
put方法好像要releaseConnection 不然 遍历调用会只调用一次 后面的 请求全堵塞了
我爱娃哈哈 2017-06-06
  • 打赏
  • 举报
回复
后端怎么知道有个客户端在请求他呢 你都没给那个服务端方法路径啊,这样的话后端方法肯定取不到参数的
李德胜1995 2017-06-06
  • 打赏
  • 举报
回复

HttpClient代码:
CloseableHttpClient httpClient=HttpClients.createDefault();
			CloseableHttpResponse response=null;
			HttpPut put=new HttpPut("url访问");
			put.setHeader("Content-type", "application/json");
			Info info=new Info("1", "2017", "傻逼");
			ObjectMapper mapper=new ObjectMapper();
			String params=null;
			try {
				params = mapper.writeValueAsString(info);
				StringEntity stringEntity=new StringEntity(params, "UTF-8");
				put.setEntity(stringEntity);
				response=httpClient.execute(put);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(httpClient!=null){
					try {
						httpClient.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(response!=null){
					try {
						response.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
Controller代码:

@RequestMapping("/路径")
	public String testUrl(@RequestBody Info info){
		System.out.println("name="+info.getName());
		return null;
	}

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧