发送httpPost请求
2021-05-17 10:29
标签:中文 获取 entity utils exe ons finally utf-8 log 发送httpPost请求 标签:中文 获取 entity utils exe ons finally utf-8 log 原文地址:http://www.cnblogs.com/linhaotown/p/7744079.html public static JSONObject sendPost(JSONObject jsonParam, String url) {
LOGGER.info("获取回执信息请求参数:"+jsonParam.toString());
JSONObject resultJson = null;
//创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
StringEntity entity=null;
if(jsonParam != null) {
entity = new StringEntity(jsonParam.toString(),"utf-8");// 解决中文乱码问题
}
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
// 发起请求
HttpResponse httpResponse = null;
String resData = null;
try {
httpResponse = client.execute(httpPost);
resData = EntityUtils.toString(httpResponse.getEntity(), Charsets.UTF_8.name());
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} finally {
try {
client.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
resultJson = JSON.parseObject(resData);
return resultJson;
}