Android 7.0 webview显示https页面空白处理方法

最近测试反馈说,到了H5页面,会显示空白页。排查后发现、由于之前的页面是http,在切换的时候,除了url之外,并未对webview的ssl校验做特殊的处理。
问题就出在这里,webview在加载https的时候,通常会用手机根证书对h5的页面进行校验,但是这个校验不一定成功。失败的时候会回调webviewclient的onReceivedSslError函数。
然后网上查了下就先给出了一个暴力的解决办法,就是在证书校验失败的情况下,将其跳过,继续加载,方法如下

@Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();  // 接受信任所有网站的证书
        }

事实也证明这个方法确实可以解决问题。。。

事后反过来思考的时候,我们切换https,本来就是想说利用ssl这一证书的校验,保证加载页面的安全性,现在在ssl校验的时候统一绕过了,不就有悖初衷了吗。然后就去看了下android.net.http.SslError这个类的源码,研究下具体的错误类型

public class SslError {

    /**
     * Individual SSL errors (in the order from the least to the most severe):
     */

    /**
     * The certificate is not yet valid
     */
    public static final int SSL_NOTYETVALID = 0;
    /**
     * The certificate has expired
     */
    public static final int SSL_EXPIRED = 1;
    /**
     * Hostname mismatch
     */
    public static final int SSL_IDMISMATCH = 2;
    /**
     * The certificate authority is not trusted
     */
    public static final int SSL_UNTRUSTED = 3;
    /**
     * The date of the certificate is invalid
     */
    public static final int SSL_DATE_INVALID = 4;
    /**
     * A generic error occurred
     */
    public static final int SSL_INVALID = 5;

    /**
     * The number of different SSL errors.
     * @deprecated This constant is not necessary for using the SslError API and
     *             can change from release to release.
     */
    // Update if you add a new SSL error!!!
    @Deprecated
    public static final int SSL_MAX_ERROR = 6;
    ...
}

其中

SslError.SSL_INVALID //webviewclient在校验ssl的过程中,出现了bug

然后就改进了上面的方法,在ssl校验失败的情况下,如果是校验过程遇到了bug,那就放过,其他情况就cancel:

@Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (error.getPrimaryError() == android.net.http.SslError.SSL_INVALID){
                handler.proceed();  // 接受信任所有网站的证书
            } else {
                handler.cancel(); // 默认操作 不处理
            } 
        }

这个就先写到这吧,后续有其他的想法再做补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DerMing_You

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值