Search code examples
androidkotlinandroid-webviewhttp-status-code-403

Why is my Android WebView showing me an 403 Forbidden Error


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


Solution

  • 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.