网上大多数解决方法针对的是以前的版本,其中有几个用到的方法已经过时,deprecation。备份。
解决的方法是创建一个能够接受所有域名的证书,然后注册到client里面去。
1 public class TestClient extends DefaultHttpClient{ 2 TestClient() throws Exception{ 3 super(); 4 SSLContext ctx = SSLContext.getInstance("TLS"); 5 X509TrustManager tm = new X509TrustManager() { 6 @Override 7 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 8 } 9 @Override 10 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 11 } 12 @Override 13 public X509Certificate[] getAcceptedIssuers() { 14 return null; 15 } 16 }; 17 ctx.init(null, new TrustManager[]{tm}, null); 18 SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 19 ClientConnectionManager ccm = this.getConnectionManager(); 20 SchemeRegistry sr = ccm.getSchemeRegistry(); 21 sr.register(new Scheme("https", 443, ssf)); 22 }
}