Zappy.Mans
Zappy.Mans

Reputation: 672

android WebView fail to load a webpage that protected by Cloudflare

I am developing an android application. There is a webview in my app, it will load a webpage.

    webView = findViewById(R.id.web_view);
    webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 SECSSOBrowserChrome");
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("xxxx.html");

I tested with https://www.google.com, the webView worked normally. But it failed to load xxx.html ( this site is protected by Cloudflare)

I added WebViewClient like this

webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    KLog.d(TAG, "onPageStarted " + url);
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    KLog.d(TAG, "host =" + request.getUrl().toString());
                    view.loadUrl(request.getUrl().toString());
                    return true;
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    KLog.d(TAG, "onPageFinished " + url);
                }

                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    super.onReceivedError(view, request, error);
                    KLog.d(TAG,"onReceivedError " + error.getDescription() + "Error code = " + error.getErrorCode());
                }
            });

But public void onPageStarted(WebView view, String url, Bitmap favicon) { never call.

After waiting for a long time, I get this image

https://drive.google.com/file/d/12DmOGWNqKcq5IsMiQEApmtnvequOIxqD/view?usp=drivesdk

Could you help me to use android WebView to load this page?

Thanks in advance

Upvotes: 3

Views: 3703

Answers (2)

DHAVAL A.
DHAVAL A.

Reputation: 2321

It is because the server you are trying to access is not secure i.e it is using HTTP (not HTTPS).

Android P uses HTTPS by default. What this means is that if you are using unencrypted HTTP requests in your app, the app will work fine in all versions of Android except Android P.

To avoid this security, try below changes in your app code.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
        ...
    </application>
</manifest>

and in res/xml add file named : network_security_config.xml

in network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        //  Add host of your URL in below line. 
        //   ie. if url is  "https://www.google.com/search?source=...."
        //   then just add "www.google.com"
        <domain includeSubdomains="true">www.myanmartvchannel.com</domain>
    </domain-config>
</network-security-config>

Upvotes: 0

Rahul Khurana
Rahul Khurana

Reputation: 8835

In the Network security configuration

Starting with Android 9 (API level 28), cleartext support is disabled by default.

To enable HTTP based URLs in the app(not recommended) add below code in the AndroidManifest.xml file in the application tag

android:usesCleartextTraffic="true"

Upvotes: 0

Related Questions