访问Http Post请求的代码示例,项目为spring boot框架,省略不重要的部分。
1、Post服务端代码:
样例1:
@RequestMapping(value ="/testDemo1",method = RequestMethod.POST,produces={"application/xml"})
public @ResponseBody String testDemo1(@RequestBody String body) throws IOException
{
logger.info(body); //通过@RequestBody 获取Post 的消息体内容,另外可自行了解@RequestParam、@RequestAttribute
return abc;
}
样例2:
@RequestMapping(value ="/testDemo",method = RequestMethod.POST,produces={"application/xml"})
public @ResponseBody String flashSingalInterface(HttpServletRequest request, HttpServletResponse response) throws IOException
{
// 读取请求内容 ----直接从request中获取
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
// 将资料解码
String reqBody = sb.toString();
String result = URLDecoder.decode(reqBody, "utf-8"); //utf-8
logger.info(result);
Map<String, String> map = new HashMap<String, String>();
map.put("resultCode", "200");
map.put("resultDesc", "nice try");