刚开始的代码如下:
SSLContext sslContext = SSLContext.getInstance("SSL");SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,new String[] {"TLSv1"}, null,SSLConnectionSocketFactory.getDefaultHostnameVerifier());
执行后异常:
java.lang.IllegalStateException: SSLContextImpl is not initialized
at sun.security.ssl.SSLContextImpl.engineGetSocketFactory(SSLContextImpl.java:172)
at javax.net.ssl.SSLContext.getSocketFactory(SSLContext.java:294)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(SSLConnectionSocketFactory.java:275)
解决办法如下:
SSLContextBuilder sslContextBuilder = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLContext sslContext =sslContextBuilder.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1.1", "SSLv3", "TLSv1", "TLSv1.2"},
null, NoopHostnameVerifier.INSTANCE);