android webview 响应头,Android中Webview打开网页的同时发送HTTP头信息方法

众所周知,当你点击一个超链接进行跳转时,WebView会自动将当前地址作为Referer(引荐)发给服务器,因此很多服务器端程序通过是否包含referer来控制盗链,所以有些时候,直接输入一个网络地址,可能有问题,那么怎么解决盗链控制问题呢,其实在webview加载时加入一个referer就可以了,如何添加呢?

从Android 2.2 (也就是API 8)开始,WebView新增加了一个接口方法,就是为了便于我们加载网页时又想发送其他的HTTP头信息的。

public void loadUrl (String url, Map 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,来展示以下如何使用。

public void testLoadURLWithHTTPHeaders() {

final String url = "http://jb51.net";

WebView webView = new WebView(getActivity());

Map extraHeaders = new HashMap();

extraHeaders.put("Referer", "http://www.google.com");

webView.loadUrl(url, extraHeaders);

}

同样上面也可以应用到UserAgent等其他HTTP头信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值