18

How can I intercept all requests made out of an Android WebView and get the corresponding request headers? I want to see the headers for every request made from my WebView including XHRs, script sources, css, etc.

The equivalent on iOS is to override NSURLCache which will give you all this information. The following are probably the closest to what I want but as far as I can see WebViewClient only ever gives us back strings:

void onLoadResource(WebView view, String url);
WebResourceResponse shouldInterceptRequest(WebView view, String url);
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • Reading the request headers is not possible with Android's `WebView` or `WebViewClient`, afaik. I solved this problem using a lightweight proxy. – acj Oct 30 '13 at 20:11
  • @acj - did you do a proxy programmatically on the `WebView` or external (router/network level)? – Mike Kwan Oct 30 '13 at 20:56
  • I configure it in Mobile Networks in the device settings. Another option: If I just need a quick-and-dirty method to check the headers, I'll start an instance of netcat (`nc`) on my laptop and connect the `WebView` to the netcat instance. – acj Oct 30 '13 at 21:14
  • If I understood correctly neither of these solutions is suitable for production deployment if I need to get these headers during the regular runs for users, right? – Mike Kwan Oct 31 '13 at 07:59
  • 1
    Correct. For regular runs, it's possible to configure a proxy programmatically using reflection ([example](http://stackoverflow.com/questions/4488338/webview-android-proxy)), but any such approach will be fragile and unsupported. – acj Oct 31 '13 at 12:53
  • Actual answer should be that starting from Android 5.0 one can use new API method `WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)`, where the `request` parameter provides the required info. – Stan Feb 22 '16 at 21:31
  • Chuck is a simple in-app HTTP inspector for Android OkHttp clients. – Akash Pal Apr 12 '21 at 10:06

4 Answers4

18
webview.setWebViewClient(new WebViewClient() {
            @SuppressLint("NewApi")
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                Log.d("", "request.getRequestHeaders()::"+request.getRequestHeaders());

                return null;
            }
        });
Mayur More
  • 951
  • 2
  • 15
  • 37
  • This was never firing on my code. I tried `Anton Prokopov`'s suggestion below and that successfully printed the headers. – PGMacDesign Sep 16 '19 at 20:35
3

You question sounds very similar to this one, so the immediate and unequivocal answer is that it is not possible to read the request headers on Android's WebView or WebViewClient.

There are workarounds, however, that could allow you get what you need. This answer describes in more detail how to do this.

Community
  • 1
  • 1
sfinja
  • 400
  • 4
  • 11
3

I also had some problems with intercepting request headers in WebView myWebView.

First I tryed to do this with setting custom WebViewClient for myWebView.

myWebView.setWebViewClient(new WebViewClient() {

    @Override
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.v("USERAGENTBROWSE", "shouldOverrideUrlLoading api >= 21 called");

                //some code

                return true;
            }

    @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.v("USERAGENTBROWSE", "shouldOverrideUrlLoading api < 21 called");

                //some code

                return true;
            }

});

But method shouldOverrideUrlLoading(...) was never called. And I have really no ideas why.

Then I found that there is the way to intercept some common headers like "User-Agent", "Cache-Control", etc.:

myWebView.getSettings().setUserAgentString("your_custom_user_agent_header");
myWebView.getSettings().setCacheMode(int mode);
...

Hope it may help somebody.

Anton Prokopov
  • 639
  • 6
  • 27
0

By this way you can enable debugging for web view application and using Chrome Developer Tools to see full details of your requests, responses, css, javascripts, etc

Fuong Lee
  • 175
  • 4