HttpClient4.2.1忽略https中的SSL證書失效


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM