AndroidWebView不使用手动安装的证书。
创始人
2024-10-12 02:33:26
0

我们可以通过以下代码来解决这个问题:

首先,我们要创建一个自定义的WebViewClient,并重写shouldOverrideUrlLoading方法。在这个方法里,我们先调用父类的shouldOverrideUrlLoading方法,然后再手动配置信任的证书。

//创建自定义的WebViewClient
private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        //调用父类的shouldOverrideUrlLoading方法
        boolean result = super.shouldOverrideUrlLoading(view, url);
        //手动配置信任的证书
        if (url != null && url.startsWith("https://")) {
            try {
                URL myUrl = new URL(url);
                HttpsURLConnection connection = (HttpsURLConnection)myUrl.openConnection();
                if (connection instanceof HttpsURLConnection) {
                    try {
                        Certificate[] certs = connection.getServerCertificates();
                        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
                        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                        trustStore.load(null);
                        for (Certificate cert : certs) {
                            String alias = cert instanceof X509Certificate ? ((X509Certificate)cert).getSubjectDN().getName() : myUrl.getHost();
                            trustStore.setCertificateEntry(alias, cert);
                        }
                        SSLContext context = SSLContext.getInstance("TLS");
                        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                        tmf.init(trustStore);
                        context.init(null, tmf.getTrustManagers(), new SecureRandom());
                        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
                        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                            @Override
                            public boolean verify(String hostname, SSLSession session) {
                                return true;
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

}

接下来,在我们的WebView中,我们要使用这个自定义的WebViewClient。这可以通过以下代码实现:

//在WebView中使用MyWebViewClient
webView.setWebViewClient(new MyWebView

相关内容

热门资讯

透视计算"wepok... 透视计算"wepoker怎么看牌型"详细辅助分享教程(透视辅助下载技巧);科技安装教程;136704...
透视挂!红龙poker透视工具... 透视挂!红龙poker透视工具,约局吧德州可以透视吗,规律教程(有挂详情)1、游戏颠覆性的策略玩法,...
透视中牌率!智星菠萝可以辅助吗... 透视中牌率!智星菠萝可以辅助吗,pokermaster脚本,必赢方法(有挂解密)1、智星菠萝可以辅助...
透视总结"德普之星透... 1、透视总结"德普之星透视辅助软件激活码"详细辅助揭秘教程(养号规律方法);详细教程。2、德普之星透...
透视免费!hhpoker透视脚... 透视免费!hhpoker透视脚本视频,We poker辅助器下载,2025新版教程(有挂辅助);进入...
透视脚本"hhpok... 1、透视脚本"hhpoker是真的还是假的"详细辅助安装教程(数据分析教程)。2、hhpoker是真...
透视中牌率!wepoekr底牌... 透视中牌率!wepoekr底牌透视,uupoker有透视吗,新2025版(有挂方法)1、在wepoe...
透视黑科技"hhpo... 透视黑科技"hhpoker透视脚本下载"详细辅助曝光教程(究竟有透视透视)1、点击下载安装,微扑克w...
透视线上!hhpoker透视脚... 透视线上!hhpoker透视脚本安卓,德普之星辅助软件,第三方教程(有挂辅助)1、实时hhpoker...
透视教程"wepok... 此外,数据分析德州(wepoker辅助工具)辅助神器app还具备辅助透视行为开挂功能,通过对客户we...