众所周知,当你点击一个超链接进行跳转时,WebView会自动将当前地址作为Referer(引荐)发给服务器,因此很多服务器端程序通过是否包含referer来控制盗链,所以有些时候,直接输入一个网络地址,可能有问题,那么怎么解决盗链控制问题呢,其实在webview加载时加入一个referer就可以了,如何添加呢?
从Android 2.2 (也就是API 8)开始,WebView新增加了一个接口方法,就是为了便于我们加载网页时又想发送其他的HTTP头信息的。
public void loadUrl (String url, Map<String, String> additionalHttpHeaders) Added in API level 8 Loads the given URL with the specified additional HTTP headers. Parameters url the URL of the resource to load additionalHttpHeaders the additional headers to be used in the HTTP request for this URL, specified as a map from name to value. Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overriden by this WebView’s defaults.
以下是一个简单的demo,来展示以下如何使用。
1 2 3 4 5 6 7 | public void testLoadURLWithHTTPHeaders() { final String url = "http://droidyue.com"; WebView webView = new WebView(getActivity()); Map<String,String> extraHeaders = new HashMap<String, String>(); extraHeaders.put("Referer", "http://www.google.com"); webView.loadUrl(url, extraHeaders); } |
---|
同样上面也可以应用到UserAgent等其他HTTP头信息 英文版文章
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有