How to view request headers in Android WebView requests?

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

The equivalent on iOS is to override NSURLCache , which will provide you with all this information. The following are probably the closest to what I want, but as far as I can see, WebViewClient only returns us the rows:

 void onLoadResource(WebView view, String url); WebResourceResponse shouldInterceptRequest(WebView view, String url); 
+7
android webview
source share
4 answers
 webview.setWebViewClient(new WebViewClient() { @SuppressLint("NewApi") @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { Log.d("", "request.getRequestHeaders()::"+request.getRequestHeaders()); return null; } }); 
+4
source share

You set sounds very similar to this , so the immediate and unequivocal answer is that it is not possible to read the request headers in Android WebView or WebViewClient .

However, there are workarounds that can allow you to get what you need. This answer details how to do this.

+3
source share

In this way, you can enable debugging for the web viewer and use the Chrome developer tools to see complete information about your requests, responses, css, javascripts, etc.

+1
source share

I also had problems intercepting request headers in the WebView myWebView .

At first I tried to do this using the WebViewClient setting 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 the shouldOverrideUrlLoading(...) method has never been called. And I really have no idea why.

Then I discovered that there is a way to intercept some common headers such as "User-Agent" , "Cache-Control" , etc.:

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

Hope this can help someone.

+1
source share

All Articles