眾所周知,當(dāng)你點(diǎn)擊一個(gè)超鏈接進(jìn)行跳轉(zhuǎn)時(shí),WebView會自動將當(dāng)前地址作為Referer(引薦)發(fā)給服務(wù)器,因此很多服務(wù)器端程序通過是否包含referer來控制盜鏈,所以有些時(shí)候,直接輸入一個(gè)網(wǎng)絡(luò)地址,可能有問題,那么怎么解決盜鏈控制問題呢,其實(shí)在webview加載時(shí)加入一個(gè)referer就可以了,如何添加呢?
從Android 2.2 (也就是API 8)開始,WebView新增加了一個(gè)接口方法,就是為了便于我們加載網(wǎng)頁時(shí)又想發(fā)送其他的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.
以下是一個(gè)簡單的demo,來展示以下如何使用。
public void testLoadURLWithHTTPHeaders() {
final String url = "http://VeVB.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);
}
同樣上面也可以應(yīng)用到UserAgent等其他HTTP頭信息。