public static String send(String url, Map<String,String> map,String encoding) throws ParseException, IOException{
String body = "";
//创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
//设置参数到请求对象中,这里使用new StringEntity()更方便,因为可传入的数据类型更多
httpPost.setEntity(new StringEntity(Map2Json(map), encoding));
System.out.println("请求地址:"+ url);
System.out.println("请求参数:"+ Map2Json(map));
//设置header信息
//指定报文头和相关数据格式
httpPost.setHeader("Content-type", "application/json");
// httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
//获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
//释放链接
response.close();
return body;
}
//这里使用Google的Gson把map类型转为json字符串比较简单
public static String Map2Json(Map<String, String> map ){
Gson gson = new Gson();
return gson.toJson(map);
}
//说明:此demo用到的相关jar包可在本人上传的TestPost.jar压缩文件中下载使用。
创建客户端发送http post请求测试他人接口方法
最新推荐文章于 2025-06-23 15:58:20 发布
本文提供了一个使用Java发送HTTP Post请求的示例代码,演示了如何构造请求、设置请求头和发送JSON格式的请求体。此外,还展示了如何处理响应并关闭连接。
2543





