向 WebView 资源请求添加自定义标头 - android [英] Add custom headers to WebView resource requests - android
问题描述
我需要向来自 WebView 的每个请求添加自定义标头.我知道 loadURL
有 extraHeaders
的参数,但这些只适用于初始请求.所有后续请求都不包含标头.我查看了 WebViewClient
中的所有覆盖,但没有任何内容允许向资源请求添加标头 - onLoadResource(WebView view, String url)
.任何帮助都会很棒.
I need to add custom headers to EVERY request coming from the WebView. I know loadURL
has the parameter for extraHeaders
, but those are only applied to the initial request. All subsequent 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)
. Any help would be wonderful.
谢谢,雷
推荐答案
尝试
loadUrl(String url, Map<String, String> extraHeaders)
要向资源加载请求添加标头,请自定义 WebViewClient 并覆盖:
For adding headers to resources loading requests, make custom WebViewClient and override:
API 24+:
WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)
or
WebResourceResponse shouldInterceptRequest(WebView view, String url)
这篇关于向 WebView 资源请求添加自定义标头 - android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!