Android WebView自定义标题 [英] Android WebView custom headers

查看:588
本文介绍了Android WebView自定义标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用这段代码为android WebView添加自定义头文件

  Map< String,String> extraHeaders = new HashMap< String,String>(); 
extraHeaders.put(example,header);
webView.loadUrl(url,extraHeader);

以上代码正在运行,但仅限于主页面。因此,如果我编写代码 echo $ _SERVER ['example'] ,它会打印标题。但在加载的URL中有一个 iframe ,当我尝试使用相同的代码时,它显示未定义的错误。有什么办法可以解决这个问题吗?

因此,我想要将自定义标题不仅添加到主要加载的URL,还添加到 iframe

解决方案

不,这不适用于Android WebView本身。你必须在你的页面代码中,或者在你的应用程序代码中,或者在服务器上工作。



为了解决这个问题,你可以使用 XMLHttpRequest 来加载子资源。但是为此,您将基本上可以即时构建页面。



在应用程序方面,您可以使用 WebViewClient.shouldInterceptRequest ,截取所有的网络请求。您不能只修改提供的请求,而是需要自己提出新请求,但您可以在其中设置任何标头。看到这个例子: Android WebViewClient url重定向(Android URL加载系统) a>



在服务器端,您可以查看子资源的 Referer 标题,其中必须包含请求它的页面。


I am currently using this code to add a custom header to android WebView

Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("example", "header");
webView.loadUrl(url, extraHeader);

Above code is working but only on the main page. So if I write this code echo $_SERVER['example'] it prints header. But there is an iframe in the loaded URL which shows an undefined error when I try the same code. Is there any way I can fix this?

So what I want to do is add custom header not only to the main loaded URL but also on the iframe of the loaded page.

解决方案

No, that is not possible with Android WebView itself. You have to work around either in your page code, or in your app's code, or on the server.

For fixing this on the page's side, you can use XMLHttpRequest for loading subresources. But for that you will have basically to construct the page on the fly.

On the app's side, you can use WebViewClient.shouldInterceptRequest, to intercept all the network requests. You are not allowed to just modify the provided request, instead, you will need to make a new request yourself, but there you will be able to set any headers you want. See this example: Android WebViewClient url redirection (Android URL loading system)

On the server side, you can look into Referer header of subresources, which must contain the url of the page that has requested it.

这篇关于Android WebView自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆