Android WebView 加载 https url 导致黑屏 [英] Android WebView load https url results in blank screen

查看:57
本文介绍了Android WebView 加载 https url 导致黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebView 可以很好地处理 http 请求和 https,其中众所周知的可信站点,例如 https://www.online.citibank.co.in/但是我尝试使用第 3 方颁发的 CA 访问私人站点,它显示空白屏幕.证书通过 SD 卡安装到手机并列在受信任的证书列表下.

WebView is working fine with http requests and also https where well known Trusted sites like https://www.online.citibank.co.in/ But I try to access private site with CA issued from 3rd party, it is giving blank screen. Certificate is installed to the phone via SD card and listed under trusted certs list.

当我在将证书添加到 TrustManager 后使用 HttpsURLConnection 尝试相同的 URL 时,它工作正常(能够获取内容).

When i tried the same URL using HttpsURLConnection after adding the cert to TrustManager, it is working fine (able to get the content).

以下是 WebView 和 HttpsURLConnection 的代码片段.

Following are the code snippet for WebView and HttpsURLConnection.

HttpsURLConnection:下面的代码工作正常并且能够从 URL 获取内容(我无法共享 URL,因为它无法从外部世界访问)

try
{
    SSLContext context = null;

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream caInput = getResources().openRawResource(R.raw.mi_net);
    Certificate ca;
    try {
        ca = cf.generateCertificate(caInput);
    } finally {
        caInput.close();
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    url = new URL(urlStr);
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();  
    con.setSSLSocketFactory(context.getSocketFactory());
    con.setInstanceFollowRedirects(true);

    con.setDoOutput(false);
    con.setConnectTimeout(1000);
    String responseMsg = con.getResponseMessage();
    response = con.getResponseCode();
    is = con.getInputStream();
}

WebView:不工作,调用回调 onReceivedSslError

{
    WebSettings viewSettings = webView.getSettings();
    viewSettings.setJavaScriptEnabled(true);
    viewSettings.setAllowContentAccess(true);
    viewSettings.setBuiltInZoomControls(false);
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.loadUrl(sameURL);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(final WebView view, final String url, Bitmap favicon) {
            Log.d("ann", "onPageStarted");

        }

        @Override
        public void onPageFinished(final WebView view, String url) {
            Log.d("ann", "inside onPageFinished");
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {

            if (!failingUrl.startsWith("mailto:")) {
                webView.loadUrl("file:///android_asset/html/error.html");
            }

        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error) {
            super.onReceivedSslError(view, handler, error);
            Log.d("ann","SSL error");

            handler.proceed();
        }

    });}
}

请帮助我提出建议.WebViewClient 异常是 I/X509Util:无法验证证书链,错误:java.security.cert.CertPathValidatorException:未找到证书路径的信任锚.

Please help me with suggestion. WebViewClient exception is I/X509Util﹕ Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

推荐答案

对于 HttpsUrlConnection,您是从文件创建证书并在运行时设置它.

For the HttpsUrlConnection you are creating the certificate from a file and setting it at runtime.

Webview 必须使用系统中已有的任何内容.

The Webview must be using whatever is in the system already.

这是一个类似的问题,并提出了解决方法:

Here is a similar question with a workaround proposed:

检查 onReceivedSslError() 如果证书是从特定的自签名 CA 签名的 WebViewClient 方法

这篇关于Android WebView 加载 https url 导致黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆