我需要为来自WebView
的每个请求添加自定义标头。我知道loadURL
具有用于添加额外标题的参数,但这些参数仅适用于部分请求。 所有(与资源有关的)请求都不包含标题。 我看过WebViewClient
中的所有覆盖项,但是没有任何内容允许向资源请求添加标头 - onLoadResource(WebView view, String url)
和shouldInterceptRequest(Webview,url)
。任何帮助将是美好的。添加来自android webview的自定义标头
4
A
回答
8
shouldInterceptRequest(Webview,url)
可以帮助您拦截每个站点的请求,例如JavaScript,CSS,Image。然后里面shouldInterceptRequest(Webview,url)
您可以使用参数url
使用HttpClient
和HttpPOST
初始新的HTTP请求,这里是示例代码:
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(<"your url for each request">);
httpPost.setHeader("<header-name>", "<header-value>");
HttpReponse httpResponse = client.execute(httpPost);
//here omit getting content-type and encoding
InputStream reponseInputStream = httpReponse.getEntity().getContent();
然后你就可以把responseInputStream
到return WebResourceResponse(<content-type>, <encoding>, reponseInputStream)
在shouldInterceptRequest(Webview,url)
,如果您有任何请求不需要添加更多标题,只需对其进行过滤即可,return null
,shouldInterceptRequest(Webview,url)
将完成其余操作。
希望这可以帮助。
相关问题
- 1. Android WebView为每个响应添加自定义HTTP标头
- 2. 为android webview添加自定义字体
- 3. 与HttpUrlConnection自定义WebViewClient - 为每个请求(WebView)添加标头
- 4. 添加自定义请求标头
- 5. 在HttpWebRequest中添加自定义标头
- 6. 为SonarQube添加自定义标头webhook
- 7. 在WCF中添加自定义标头?
- 8. 为Volley库添加自定义标头
- 9. 在Android中添加自定义标记
- 10. Android Webview - 自定义HTML
- 11. Android WebView自定义错误
- 12. 添加自定义VIew- Android
- 13. 添加自定义SoapClient的头
- 14. mod_deflate的自定义标头
- 15. 在自定义会员供应商中添加自定义标头
- 16. Laravel不添加自定义头文件
- 17. android webview无法添加自定义字体?
- 18. SUDS自定义标头
- 19. 自定义HTTP标头UIWebview
- 20. 自定义http标头
- 21. Pico CMS自定义标头
- 22. jQuery AJAX自定义标头
- 23. 自定义webix标头
- 24. redirect_to自定义http标头
- 25. 添加自定义标头与经典的ASP
- 26. 来自于Android的WebView
- 27. 在webview中的自定义键盘android
- 28. 添加自定义主题中的Android
- 29. 添加自定义命令(自定义图标)
- 30. 如何覆盖Android WebView以使用自定义的Accept-Language标头?
[向WebView资源请求添加自定义标头的可能的重复项](http://stackoverflow.com/questions/7610790/add-custom-headers-to-webview-resource-requests-android) – d60402