【问题标题】:Android WebView blocks redirect from https to httpAndroid WebView 阻止从 https 重定向到 http
【发布时间】:2015-04-21 23:33:36
【问题描述】:

我有一个解决方案,我的 Android WebView 需要首先打开一个 https url,然后它将被重定向到一个 http url(它可能正在尝试来自 https 站点的 http POST)。这不起作用,我的 Android 调试日志说:

02-20 11:04:45.079 8538-8538/? E/WebViewCallback:被阻止的 URL:[阻止]“https://xxx/”处的页面是通过 HTTPS 加载的,但正在将数据提交到“http://yyy”处的不安全位置:此内容也应通过 HTTPS 提交。

WebView 中是否有任何配置选项允许这种行为?

更多信息:这似乎是 Android SDK 中的行为更改。一个很久以前编译的客户就这样做了,没有任何抱怨。

【问题讨论】:

    标签: android webview https


    【解决方案1】:

    Lollipop (API 20) 中混合 http/https 内容的默认 WebView 设置发生了变化。详情请见https://datatheorem.github.io/android/2014/12/20/webviews-andorid-lollipop/

    要允许 https 重定向到 http,您需要将混合内容模式设置为 MIXED_CONTENT_ALWAYS_ALLOW

     if (Build.VERSION.SDK_INT >= 21) {
            webview.getSettings().setMixedContentMode( WebSettings.MIXED_CONTENT_ALWAYS_ALLOW );
        }
    

    请注意,从安全角度来看,设置 MIXED_CONTENT_ALWAYS_ALLOW 是不好的,正如您在回答中指出的那样,最好在两个站点上都支持 https。

    但对于那些无法控制网站的人来说,这应该可行。

    【讨论】:

    • 救命!非常感谢:)
    【解决方案2】:

    您可以通过覆盖 onReceivedSslError() 方法来忽略 ssl 错误。

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }
    

    希望它对你有用。

    【讨论】:

    • 感谢您的回答。尽管它可能有效(尚未尝试过),但禁用所有 SSL 错误似乎是一个可怕的想法。
    • 是的,你是对的。这完全违背了拥有 SSL 的目的。我没有找到比上面更好的方法。检查 。 (code.google.com/p/android/issues/detail?id=2388#c15)
    • 请注意,Google 不允许您使用此代码发布您的应用
    【解决方案3】:

    根据我的研究,我认为禁用此功能是不可能的。我将在两个站点中都支持 https。反正最安全。

    【讨论】:

      【解决方案4】:

      它对我有用

       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.webView.getContext());
        AlertDialog alertDialog = builder.create();
        String message = "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?";
        alertDialog.setTitle("SSL Certificate Error");
        alertDialog.setMessage(message);
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
      
           public void onClick(DialogInterface dialog, int which) {
              Log.d("CHECK", "Button ok pressed");
              // Ignore SSL certificate errors
              handler.proceed();
           }
        });
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
      
           public void onClick(DialogInterface dialog, int which) {
              Log.d("CHECK", "Button cancel pressed");
              handler.cancel();
           }
        });
        alertDialog.show();
      

      【讨论】:

        猜你喜欢
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-10
        • 2017-10-06
        • 2018-05-23
        相关资源
        最近更新 更多