import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
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 javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpsSSLClient {
/**
* 获取Https 请求客户端
* @return
*/
public static CloseableHttpClient createSSLInsecureClient() {
SSLContext sslcontext = createSSLContext();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new HostnameVerifier() {
@Override
public boolean verify(String paramString, SSLSession paramSSLSession) {
return true;
}
});
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
return httpclient;
}
/**
* 获取初始化SslContext
* @return
*/
private static SSLContext createSSLContext() {
SSLContext sslcontext = null;
try {
sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] {new TrustAnyTrustManager()}, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslcontext;
}
/**
* 自定义静态私有类
*/
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
}
}
public static String doGet(String url, String charSet) throws Exception {
// When an instance CloseableHttpClient is no longer needed and is about to go out
// of scope the connection manager associated with it must be shut down by calling the
// CloseableHttpClient.close() method.
if (StringUtils.isBlank(url)) {
throw new Exception("The request url is blank.");
}
CloseableHttpClient httpClient = null;
if (StringUtils.startsWith(url, "https")) {
httpClient = HttpsSSLClient.createSSLInsecureClient();
} else {
httpClient = HttpClients.createDefault();
}
HttpGet getMethod = new HttpGet(url);
return doGet(url, charSet, httpClient, getMethod);
}
private static String doGet(String url, String charSet, CloseableHttpClient httpClient, HttpGet getMethod)
throws Exception {
charSet = StringUtils.isBlank(charSet) ? "UTF-8" : charSet;
CloseableHttpResponse response = null;
HttpEntity responseEntity = null;
try {
response = httpClient.execute(getMethod);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state != HttpStatus.SC_OK) {
throw new Exception("响应编码:"+state);
}
responseEntity = response.getEntity();
return EntityUtils.toString(responseEntity, charSet);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
EntityUtils.consume(responseEntity);
} catch (IOException e) {
e.printStackTrace();
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}