WebView
对android 的控制,是否支持SSL?
我正在尝试加载一个使用受信任的ssl证书的网页,但WebView
它只是白色.
有什么建议?
不是专家,只是我在网上找到的东西.根据我的理解,WebView确实支持ssl,但是,空白屏幕表明WebView不相信证书是有效的.这可能发生在自签名的证书或未在android中设置的root身份验证(完全有效的证书不验证).无论如何,如果您使用froyo或更好,您可以尝试以下方式:
import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.SslErrorHandler; import android.net.http.SslError; ... engine = (WebView) findViewById(R.id.my_webview); engine.setWebViewClient(new WebViewClient() { @Override public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); } });
要根据更新的安全策略正确处理SSL证书验证,请根据服务器提供的证书更改代码以调用SslErrorHandler.proceed(),否则调用SslErrorHandler.cancel().
例如,我添加了一个警告对话框,以便用户确认并且Google似乎不再显示警告.
@Override public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) { final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); String message = "SSL Certificate error."; switch (error.getPrimaryError()) { case SslError.SSL_UNTRUSTED: message = "The certificate authority is not trusted."; break; case SslError.SSL_EXPIRED: message = "The certificate has expired."; break; case SslError.SSL_IDMISMATCH: message = "The certificate Hostname mismatch."; break; case SslError.SSL_NOTYETVALID: message = "The certificate is not yet valid."; break; } message += " Do you want to continue anyway?"; builder.setTitle("SSL Certificate Error"); builder.setMessage(message); 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(); }
更改后,它不会显示警告.