我是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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题