post请求和接收

时间:2017-07-29 12:35:06   收藏:0   阅读:212

请求

String url = "";
try {
JSONObject jsonParam = new JSONObject();
jsonParam.put("type", type);
String sign = Md5Util.string2MD5(jsonParam.toString());
jsonParam.put("sign", sign);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
if (null != jsonParam) {
//解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
/**请求发送成功,并得到响应**/
System.out.println("result:"+result.getStatusLine().getStatusCode());
System.out.println("url:"+url);
if (result.getStatusLine().getStatusCode() == 200) {
System.out.println("成功");
}
} catch (IOException e) {
System.out.println("post请求提交失败:" + url);
e.printStackTrace();
}

 

 

 

接收

@RequestMapping(value = "aaa",method=RequestMethod.POST)

public String wxShopPay(@RequestBody String requestBody, HttpServletRequest request){
JSONObject jsonObject = JSONObject.fromObject(requestBody);

 String type = jsonObject.getString("type");  

}

原文:http://www.cnblogs.com/tianhao2017/p/7255428.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!