How do I see request headers on Android WebView requests?

一笑奈何 提交于 2020-01-23 04:58:04

问题


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);

回答1:


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

                return null;
            }
        });



回答2:


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.




回答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.




回答4:


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



来源:https://stackoverflow.com/questions/19692617/how-do-i-see-request-headers-on-android-webview-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!