importorg.apache.commons.lang.StringUtils;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.StatusLine;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.HttpResponseException;importorg.apache.http.client.ResponseHandler;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util.EntityUtils;importjavax.net.ssl.SSLContext;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importjava.io.IOException;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;public classPostUtil {/*** 发送post请求
*@paramurl 请求地址
*@paramjson json格式字符串
*@paramcontentType 这里用 "application/json"
*@return
*/
public staticString post(String url, String json, String contentType) {
HttpClientBuilder httpClientBuilder=HttpClientBuilder.create();//HttpClient
CloseableHttpClient client =httpClientBuilder.build();
client=(CloseableHttpClient) wrapClient(client);
HttpPost post= newHttpPost(url);try{
StringEntity s= new StringEntity(json, "utf-8");if(StringUtils.isBlank(contentType)) {
s.setContentType("application/json");
}
s.setContentType(contentType);
post.setEntity(s);
HttpResponse res=client.execute(post);
HttpEntity entity=res.getEntity();
String str= EntityUtils.toString(entity, "utf-8");returnstr;
}catch(Exception e) {
e.printStackTrace();
}return null;
}private staticorg.apache.http.client.HttpClient wrapClient(HttpClient base) {try{
SSLContext ctx= SSLContext.getInstance("TLSv1");
X509TrustManager tm= newX509TrustManager() {public voidcheckClientTrusted(X509Certificate[] xcs,
String string)throwsCertificateException {
}public voidcheckServerTrusted(X509Certificate[] xcs,
String string)throwsCertificateException {
}publicX509Certificate[] getAcceptedIssuers() {return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLConnectionSocketFactory sslsf= new SSLConnectionSocketFactory(ctx, new String[]{"TLSv1"}, null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient=HttpClients.custom().setSSLSocketFactory(sslsf).build();returnhttpclient;
}catch(Exception ex) {return null;
}
}public class CharsetHandler implements ResponseHandler{privateString charset;publicCharsetHandler(String charset) {this.charset =charset;
}publicString handleResponse(HttpResponse response)throwsClientProtocolException, IOException {
StatusLine statusLine=response.getStatusLine();if (statusLine.getStatusCode() >= 300) {throw newHttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
HttpEntity entity=response.getEntity();if (entity != null) {if (!StringUtils.isBlank(charset)) {returnEntityUtils.toString(entity, charset);
}else{returnEntityUtils.toString(entity);
}
}else{return null;
}
}
}
}