ignore ssl errors in DefaultHttpClient

前端 未结 1 1728
一向
一向 2020-12-29 00:38

Im searching for a possibility to ignore all ssl errors (eg. not trusted) in a default httpclient. I\'ve seen lots of solutions here, but i alwas have to import a specific c

相关标签:
1条回答
  • 2020-12-29 01:10

    I solved the problem. It works if you use the above request, but instead of the DefaultHttpClient, use your own version:

    public class MyHttpClient extends DefaultHttpClient {
    final Context context;
    TrustManager easyTrustManager = new X509TrustManager() {
        @Override
        public void checkClientTrusted(
                X509Certificate[] chain,
                String authType) throws CertificateException {
        }
    
        @Override
        public void checkServerTrusted(
                X509Certificate[] chain,
                String authType) throws CertificateException {
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }    
    };
      public MyHttpClient(Context context) {
        this.context = context;
      }
    
      @Override protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(
            new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
      }
    
    
      private MySSLSocketFactory newSslSocketFactory() {
        try {
          KeyStore trusted = KeyStore.getInstance("BKS");      
          try {
             trusted.load(null, null);
    
          } finally {
          }
    
          MySSLSocketFactory sslfactory =  new MySSLSocketFactory(trusted);
            sslfactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            return sslfactory;
        } catch (Exception e) {
          throw new AssertionError(e);
        }
    
      }
      public class MySSLSocketFactory extends SSLSocketFactory {
            SSLContext sslContext = SSLContext.getInstance("TLS");
    
            public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
                super(truststore);
    
                TrustManager tm = new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    }
    
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    }
    
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                };
    
                sslContext.init(null, new TrustManager[] { tm }, null);
            }
    
            @Override
            public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
                return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
            }
    
            @Override
            public Socket createSocket() throws IOException {
                return sslContext.getSocketFactory().createSocket();
            }
        }
       }
    
    0 讨论(0)
提交回复
热议问题