android WebView fail to load a webpage that protected by Cloudflare

跟風遠走 提交于 2019-12-11 17:28:22

问题


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


回答1:


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"



回答2:


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>


来源:https://stackoverflow.com/questions/57984976/android-webview-fail-to-load-a-webpage-that-protected-by-cloudflare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!