網上大多數解決方法針對的是以前的版本,其中有幾個用到的方法已經過時,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 }
}