public static String doPostRequest(String url, Map<String,String> header,
JSONObject params, HttpEntity entity){
String resultStr = "";
if(StringUtils.isEmpty(url)){
return resultStr;
}
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try{
httpClient = SSLClientCustom.getHttpClient();
HttpPost httpPost = new HttpPost(url);
if(!header.isEmpty()){
for(Map.Entry<String,String> stringStringEntry : header.entrySet()){
httpPost.setHeader(stringStringEntry.getKey(),stringStringEntry.getValue());
}
}
if (!params.isEmpty()){
StringEntity stringEntity = new StringEntity(params.toString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");
httpPost.setEntity(stringEntity);
}
if(entity != null){
httpPost.setEntity(entity);
}
httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK){
HttpEntity httpResponseEntity = httpResponse.getEntity();
resultStr = EntityUtils.toString(httpResponseEntity);
logger.info("请求正常,请求地址:{},响应结果:{}",url,resultStr);
}else {
StringBuffer stringBuffer = new StringBuffer();
HeaderIterator headerIterator = httpResponse.headerIterator();
while (headerIterator.hasNext()){
stringBuffer.append("\t"+headerIterator.next());
}
logger.info("异常信息:请求地址:{},响应状态:{},请求返回结果:{}",url,httpResponse.getStatusLine().getStatusCode(),stringBuffer);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
httpResponse.close();
httpClient.close();
}catch (Exception e){
e.printStackTrace();
}
}
return resultStr;
}