public void inform(String sj){
String url = "http://127.0.0.1/xxx";
HttpPost httpPost = new HttpPost(url);
try {
if (StringUtil.isNotEmpty(sj)){
JSONObject jsonObject = new JSONObject();
jsonObject.put("sj",sj);
httpPost.addHeader("Content-type", "application/json");
httpPost.addHeader("xxx",xxx);
httpPost.addHeader("xxx",xxx);
StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse result = httpClient.execute(httpPost);
String str = "";
JSONObject object = null;
str = EntityUtils.toString(result.getEntity(), "utf-8");
object = JSONObject.parseObject(str);
logger.info("接口响应数据 :" + object);
}
}catch (Exception e){
logger.error("Service inform e = " + e);
} finally {
httpPost.releaseConnection();
}
}
public static JSONObject httpPostToJson(String url, String param) {
JSONObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
if (StringUtil.isNotEmpty(param)) {
StringEntity entity = new StringEntity(param, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
String str = EntityUtils.toString(result.getEntity(), "utf-8");
jsonResult = JSONObject.parseObject(str);
logger.info("httpPost result:{}",result.toString());
} catch (IOException e) {
logger.error("post请求提交失败:" + url, e);
} finally {
httpPost.releaseConnection();
}
return jsonResult;
}