android-访问https

这里写自定义目录标题

  • okhttp使用https忽略证书验证
  • webview 加载https --忽略证书

okhttp使用https忽略证书验证

		final SSLSocketFactory sslSocketFactory ;
        try {
            SSLContext sslContext;
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null,new X509TrustManager[]{trustManager},null);
            sslSocketFactory = sslContext.getSocketFactory();
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }

    X509TrustManager trustManager = 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 new X509Certificate[0];
        }
    };

    HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

//设置okhttp的两个属性 
okHttpClient.setHostnameVerifier(DO_NOT_VERIFY); 
okHttpClient.setSslSocketFactory(sslSocketFactory);

webview 加载https --忽略证书

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        //handler.cancel();// 默认的处理方式,WebView变成空白页
        handler.proceed();//忽略证书的错误继续Load页面内容,不会显示空白页面
        // super.onReceivedSslError(view, handler, error);
    }
});

你可能感兴趣的:(android)