7

I am using a Webview with WebViewClient to load a URL which requires authentication. I am using onReceivedHttpAuthRequest method of WebViewClient to set the authentication for every request.

@Override
    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
        handler.proceed("username", "password");
    }

This is working fine for the URL's which need only basic authentication.

However some of the URL's in my app, need login access with some extra headers in request. So for this kind of requests the basic authentication is not enough and the server will throw 401 auth error.

So for this scenario, I need to catch 401 error and need to navigate user to the login screen. From marshmallow onward WebViewClient has provided below method to handle such type of HTTP errors.

    @Override
    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
        super.onReceivedHttpError(view, request, errorResponse);
        if(errorResponse.getStatusCode() ==401){
            // Code to navigate user to Login Screen.
        }
    }

But I didn't find any provision for below marshmallow devices to handle or to get a callback for such a 401 errors. Also the callback method onReceivedError is not working either.

Any help would be appreciated.

Ajinkya678
  • 101
  • 1
  • 7

0 Answers0