Android应用中WebView访问https SSL证书网页时,如果访问的网站证书无效或已过期,则会提示如上图界面,通过重写webViewClinet中的onReceivedSslError方法可
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if (handler != null) {
handler.proceed();//忽略证书的错误继续加载页面内容,不会变成空白页面
}
}
上述方法较为粗暴,直接忽略错误直接进入界面,也可采取下述方法,弹框提示用户,是否忽略SSL错误,继续访问网页。
@Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error{
final AlertDialog.Builder builder = new AlertDialog.Builder(WechatLoginActivity.this);
builder.setMessage(R.string.notification_error_ssl_cert_invalid);
builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
Comments | NOTHING