2

I'm having some trouble getting my webview to work properly

The webview is supposed to display this url https://mcdonalds.de/deinfeedback , but whenever I try to load it my onReceivedHttpError method returns me an

403 Forbidden

Error. It works just fine for other urls like https://www.google.com, but i can't seem to make it work for this one. At first I thought it had something to do with the ssl certificates, however my WebViewClient's onReceiveSslError is never called. I then tried to change my user agent string to the same one I have on my mobile browser but it still returned me the same 403 error

This is the part in my onCreate where I deal with the WebView

webViewDisplay.settings.builtInZoomControls = true
webViewDisplay.settings.displayZoomControls = false
webViewDisplay.settings.domStorageEnabled   = true
webViewDisplay.settings.javaScriptEnabled = true
webViewDisplay.settings.javaScriptCanOpenWindowsAutomatically = true
webViewDisplay.webViewClient = MyWebClient()

webViewDisplay.loadUrl(resources.getString(R.string.web_activation_url_main)) //https://www.mcdonalds.de/deinfeedback

And this is my WebViewClient

inner class MyWebClient : WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
            Log.v(TAG, "Overriding Url to ${request.url.toString()}")
            return false //continues loading
        }


        override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler, error: SslError?) {
            Log.v(TAG, "Ssl Error")
            handler.proceed()  //handle error
        }


        override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse) {
            Log.v(TAG, "Http Error   ${errorResponse.statusCode} ${errorResponse.reasonPhrase}")
            super.onReceivedHttpError(view, request, errorResponse)
        }

    }

I hope somebody can help me with this


Edit:

The 403 Error was not the issue. I had set the height of my layout to wrap_content which makes the webView wrap itself on some sites. Changing it to match_parent fixed it. Credit for this goes to Dani

robb
  • 23
  • 2
  • 6

2 Answers2

1

Are you sure its a WebView issue only? Executing this url in a desktop browser getting one 403 - and seeing exactly the same content as I do in android's WebView. In other words, if you are seeing the same, I think you can safely ignore the error. If you are seeing something else, please attach a snapshot.

enter image description here

Edit: Pretty certain now.

Adding:

 @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse) {
            Log.v("APP", "Http Error ${request?.url}   ${errorResponse.responseHeaders} ${errorResponse.reasonPhrase}")
            super.onReceivedHttpError(view, request, errorResponse)
        }

Produces this: Http Error https://s3-eu-west-1.amazonaws.com/automation.isc-mcd.svy.do--voc/public/de/.json {Transfer-Encoding=chunked, Server=AmazonS3, Access-Control-Allow-Origin=*, Access-Control-Allow-Methods=GET, Vary=Origin, Access-Control-Request-Headers, Access-Control-Request-Method, x-amz-request-id=AC63B0D940D45EDA, Access-Control-Max-Age=3000, x-amz-id-2=7LKt0WZsaKdyYTQhN5cSmGSdUZMBR+D8mEaAOovorrV5jNJcS0CvNFb08K7QqnTkn4C73MfMWJI=, Date=Fri, 03 Jan 2020 03:17:05 GMT, Content-Type=application/xml} Forbidden

Note this is exactly the same request that results in Forbidden in desktop browsers too. No harmful side effects, can be ignored.

Dmitri
  • 2,563
  • 1
  • 22
  • 30
  • You're absolutely right, the 403 error was not the issue. I had set the height of my web view to wrap_content which makes the web view wrap itself on some sites and on some not. Thanks for the help :) – robb Jan 03 '20 at 14:18
0

To resolve 403 forbiddon error, simply update your webview userAgentString.

here is the example:

Java:

String commenUserAgent = "Chrome/56.0.0 Mobile";
yourWebView.getSettings().setUserAgentString(commenUserAgent);

Kotlin:

val commenUserAgent = "Chrome/56.0.0 Mobile"
yourWebView.settings.userAgentString = commenUserAgent

if you want more user agent you can use:

"Mozilla/5.0 AppleWebKit/535.19 Chrome/56.0.0 Mobile Safari/535.19"