前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Webview加载网页时发送HTTP头信息

Android Webview加载网页时发送HTTP头信息

作者头像
技术小黑屋
发布2018-09-04 16:43:07
1.4K0
发布2018-09-04 16:43:07
举报
文章被收录于专栏:技术小黑屋

众所周知,当你点击一个超链接进行跳转时,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头信息 英文版文章

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档