Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView blocks redirect from https to http

I have a solution where my Android WebView needs to first open a https url, then it will be redirected to a http url (it might be trying a http POST from the https site). This is not working, and my Android debug log says:

02-20 11:04:45.079 8538-8538/? E/WebViewCallback﹕ Blocked URL: [blocked] The page at 'https://xxx/' was loaded over HTTPS, but is submitting data to an insecure location at 'http://yyy': this content should also be submitted over HTTPS.

Are there any configuration options in the WebView that will allow this behaviour?

More info: it seems like a behaviour change in the Android SDK. A client compiled a long time ago does this without any complaints.

like image 548
Kenneth Avatar asked Feb 20 '15 10:02

Kenneth


People also ask

How do you prevent the WebView from invoking the device's Web browser when a redirection occurs in the WebView?

To detect and intercept any redirection from WebView , we can use shouldOverrideUrlLoading and return true if it is supported to redirect into native page so that WebView stop the URL redirection in the web page and stay in the current page.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.

How do you intercept a URL in WebView?

Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead. if the tap in Webview is a link to custom URL, (like myurl://parameters), its not getting called.

Do cookies work in WebView?

Bookmark this question. Show activity on this post. I have an application on appspot that works fine through regular browser, however when used through Android WebView, it cannot set and read cookies.


2 Answers

There was a change in default WebView settings for mixed http/https content in Lollipop (API 20). See https://datatheorem.github.io/android/2014/12/20/webviews-andorid-lollipop/ for more details.

To allow https to redirect to http you need to set the mixed content mode to MIXED_CONTENT_ALWAYS_ALLOW

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

Note that setting MIXED_CONTENT_ALWAYS_ALLOW is bad from security point of view, and as you note in your answer, it is better to support https on both sites.

But for those that don't have control over the sites, this should work.

like image 187
Anton G Avatar answered Sep 18 '22 01:09

Anton G


You can ignore ssl error by overriding onReceivedSslError() method.

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

Hope it will be work for you.

like image 27
Arkar Aung Avatar answered Sep 20 '22 01:09

Arkar Aung