I need to add custom headers to EVERY request coming from the WebView
. I know loadURL
has the parameter for adding extra Headers, but those are only applied to only some of the requests.
All (resource related) requests do not contain the headers.
I have looked at all overrides in WebViewClient
, but nothing allows for adding headers to resource requests - onLoadResource(WebView view, String url)
and shouldInterceptRequest(Webview,url)
. Any help would be wonderful.
Asked
Active
Viewed 2.0k times
5

Pranav Singh
- 17,079
- 30
- 77
- 104

Gopichand
- 51
- 1
- 1
- 4
1 Answers
9
shouldInterceptRequest(Webview,url)
can help you to intercept every request of a site, such as JavaScript, CSS, Image. Then inside shouldInterceptRequest(Webview,url)
you can use the parameter url
to initial new http request by using HttpClient
and HttpPOST
, here is example code :
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(<"your url for each request">);
httpPost.setHeader("<header-name>", "<header-value>");
HttpReponse httpResponse = client.execute(httpPost);
//here omit getting content-type and encoding
InputStream reponseInputStream = httpReponse.getEntity().getContent();
Then you can put responseInputStream
to return WebResourceResponse(<content-type>, <encoding>, reponseInputStream)
in your shouldInterceptRequest(Webview,url)
if you have any request which doesn't need add more header, just filter it and return null
, shouldInterceptRequest(Webview,url)
will do the rest.
Hope this can help.

Martin Konecny
- 57,827
- 19
- 139
- 159

admar.kevin
- 376
- 3
- 7
-
We are developing a browser which sends request to proxy and proxy sends data back to browser.So, every request should go to proxy with custom header and shows the page from proxy.In this, how can we intercept only resource related requests.Resources types are so many, like CSS,JS,PNG,JPEG,etc... So, how can I handle this. – Gopichand Aug 26 '13 at 07:29
-
only resource related requests, I suppose you dont want to add headers to web page itself, you can just filter it in `ShouldInterceptRequest()` using request's content-type other than "text/html". – admar.kevin Aug 26 '13 at 09:25
-
How can I get content-type from request? As far as I know, we can get only from response. – Gopichand Aug 27 '13 at 05:06
-
Take a look at [here](http://developer.android.com/reference/org/apache/http/HttpEntity.html#getContentType()). Using `httpResponse.getEntity()` to get the `HttpEntity`. – admar.kevin Aug 27 '13 at 18:10
-
shouldInterceptRequest() is not called for repeated resource urls hence not able to add my custom header. for e.g. for the first time when url1 is called shouldInterceptRequest() is called but for the second time when url1 is called shouldInterceptRequest() is not called. – saurav Sep 02 '14 at 09:46
-
@admar.kevin What is HttpResponse? What library are you getting this from. It's not available for me with default Android SDK. – b.lyte Mar 18 '16 at 18:29
-
@clu please refer to "Apache HTTP Client Removal Section" in http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html – admar.kevin Mar 19 '16 at 17:10