我是1只猫 2017-07-10 07:10 采纳率: 0%
浏览 22721

java Post请求接收body里的参数

try {
URL url = new URL(strURL);// 创建连接
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST");// 设置请求方式

connection.setRequestProperty("Accept","application/json");// 设置接收数据的格式

connection.setRequestProperty("Content-Type","application/json");// 设置发送数据的格式

connection.connect(); OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream(),"UTF-8");// utf-8编码

out.append(params); out.flush(); out.close(); // 读取响应

int length = (int) connection.getContentLength();// 获取长度

InputStream is = connection.getInputStream();
if (length != -1){
byte[] data = new byte[length];
byte[] temp = new byte[512];
int readLen = 0; int destPos = 0;
while ((readLen = is.read(temp)) > 0){
System.arraycopy(temp, 0, data, destPos, readLen);
destPos += readLen;
}
String result = new String(data, "UTF-8");
System.out.println(result);
return result;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

    --接收方法
    @RequestMapping(value="/test", method = RequestMethod.POST)

@ResponseBody
public String test(HttpServletRequest request,HttpServletResponse response,@RequestBody String str){
System.out.println(str);

return "DetailedRules";

}

  • 写回答

2条回答 默认 最新

  • 白萝卜。 2017-07-10 09:51
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器