Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to modify the value of the key 'X-requested-with' in the request headers of resource request in Android webview, but not a page request?

I have met a problem when I capture the http package when my webView visit a url. the package shows that the request send by ajax has a 'X-requested-with' key in the headers, which has the value of my app package name like 'com.xxx'. I don't want the url I visited to know the request came from my app, so I need to replace the key 'X-requested-with' to another value.but I tried the sloution in Add custom headers to WebView resource requests - android, all of them do not work.

loadUrl(Strring url, Map<String, String> extraHeaders) will be called only in the page request, not in the resource request send by ajax.shouldOverrideUrlLoading(WebView view, String url) and WebViewClient.shouldInterceptRequest(android.webkit.WebView view, java.lang.String url) will be called in every request, but I can't modify headers in them. So any solutions to the question? Thank you so much.

like image 317
blank_ken Avatar asked Mar 22 '23 19:03

blank_ken


1 Answers

well, finally I solve the question. I can't find out the way to modify the headers in the request send by ajax in an loading page still, but I chose to override public String getPackageName() in Application. I look up the callingstack, if the calling comes from android.webkit, I return another string instead of my package name. So my issues solved. But the basic question still leaves.

like image 60
blank_ken Avatar answered Apr 07 '23 00:04

blank_ken