代码自己看,看不懂的私信我
package com.dst.slms.app.utils;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
/**
*
* @author chenmin
*
*/
public class HttpClientUtil {
public static CloseableHttpClient createSSLClient(){
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null,new TrustStrategy(){
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//通过所有证书
return true;
}
}).build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier(){
@Override
public boolean verify(String arg0, SSLSession arg1) {
// 不用验证hostname
return true;
}
});
//如果异常了,创建普通的client
return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
} catch (Exception e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}
}
package com.dst.slms.app.utils;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
public class HttpUtil {
static Logger log = LoggerFactory.getLogger(HttpUtil.class);
public static String httpPost(String url, Object data) {
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClientUtil.createSSLClient();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
try {
StringEntity requestEntity = new StringEntity(JSON.toJSONString(data), "utf-8");
httpPost.setEntity(requestEntity);
response = httpClient.execute(httpPost, new BasicHttpContext());
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity == null) {
return null;
}
String resultStr = EntityUtils.toString(entity, "utf-8");
String var10 = resultStr;
return var10;
}
log.error("request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + url);
} catch (IOException var20) {
log.error("request url=" + url + ", exception, msg=" + var20.getMessage());
var20.printStackTrace();
return null;
} finally {
if (response != null) {
try {
response.close();
} catch (IOException var19) {
var19.printStackTrace();
}
}
}
return null;
}
}