@RequestMapping(path = "post")
public JSONObject post() throws IOException {
JSONObject result = this.start();
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("url调用的接口地址");
PushParams pushParams = new PushParams();
pushParams.setMessagesJson(JSON.toJSONString(list.get(i)));
pushParams.setTitle(title);//参数1
pushParams.setContent(content);//参数2
//System.out.println("json:" + json);
post.addRequestHeader("Content-type", "application/json; charset=utf-8");
post.setRequestHeader("Accept", "application/json");
post.setRequestEntity(new ByteArrayRequestEntity(json.getBytes("UTF-8")));
client.executeMethod(post);
InputStream str = post.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(str, "UTF-8"));
String tempbf;
StringBuffer html = new StringBuffer();
while ((tempbf = br.readLine()) != null) {
html.append(tempbf);
}
String sendResult = html.toString();
System.out.println(sendResult);//接口返回结果
JSONObject j = JSONObject.parseObject(sendResult);
return this.finish(result);
}
Java模拟发送post请求接口
最新推荐文章于 2024-05-10 14:52:23 发布