首页 > 解决方案 > 在android webiew中向请求添加标头

问题描述

我有一个必须在 Web 视图中加载的 url,但我需要将标头传递给请求。

我尝试使用 HashMap 将标题设置为 Web 视图,但没有奏效。

我找到了诸如 web 视图拦截客户端之类的解决方案,但没有得到如何加载 url 的正确示例。任何人都可以建议如何实现它。

标签: androidwebviewhttp-headers

解决方案


尝试

loadUrl(String url, Map<String, String> extraHeaders)

要向资源加载请求添加标头,请自定义 WebViewClient 并覆盖:

API 24+:
WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)
or
WebResourceResponse shouldInterceptRequest(WebView view, String url)

推荐阅读