Webview override default headers like accept-language, accept-encoding, accept-charset, etc

Viewed 2844

Well. The title is self explanatory. I want to override EVERY header from any WebView request (POST or GET). The method provided by Android API is not overriding those headers. Although I set them, the default headers prevail.

For example:

Map<String, String> headers = new HashMap<String, String>();
headers.put("Referer", "www.hello.com");
headers.put("User-Agent", "Any User Agent");
headers.put("Accept-Language", "es");
webView.loadUrl("www.hello.com/world.html", headers);

That doesn't work. After running that (well, not exactly that! that was an example) in the emulator and viewving the traffic with Wireshark I can confirm that only the default headers are sent. My custom headers are discarded.

The only custom header that prevails is the User-Agent when it's set this way:

webView.getSettings().setUserAgentString("Any User Agent");

Using the loadUrl(url, extraheaders) method it doesn't work.

So. I'm desesperatly looking for a way of changing the headers of my webview. There SHOULD be a way. According to the "internetz" shouldOverrideUrlLoading and shouldinterceptrequest don't catch POSTS. I need to catch POSTS and GET. I want to change the headers of EVERY request that comes out of my webview. It should be simple! But I can't find a simple solution.

Any ideas?

Thanks.

EDIT 1:

I've even tried changing the locale within my application. It kind of worked. At least I fooled WebView to put my own Accept-Language. But for some reason, the people behind Android thought that having "en-US" in the Accept-Language header is a MUST HAVE. No matter what language you do speak.

I changed my locale to es-AR and it worked. But WebView added en-US too. So now my Accept-Language is "es-AR, en-US" which is unacceptable for me (don't ask me why please).

This is the source of the webview part where it gets the locale and generates the accept-langauge header. Notice the part where, if your locale is not set to US, it forces the accept-language to en-US as a "second language".

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/webkit/WebSettings.java#WebSettings.getCurrentAcceptLanguage%28%29

0 Answers
Related