Android中Webview打開網(wǎng)頁的同時發(fā)送HTTP頭信息方法
眾所周知,當(dāng)你點擊一個超鏈接進行跳轉(zhuǎn)時,WebView會自動將當(dāng)前地址作為Referer(引薦)發(fā)給服務(wù)器,因此很多服務(wù)器端程序通過是否包含referer來控制盜鏈,所以有些時候,直接輸入一個網(wǎng)絡(luò)地址,可能有問題,那么怎么解決盜鏈控制問題呢,其實在webview加載時加入一個referer就可以了,如何添加呢?
從Android 2.2 (也就是API 8)開始,WebView新增加了一個接口方法,就是為了便于我們加載網(wǎng)頁時又想發(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.
以下是一個簡單的demo,來展示以下如何使用。
public void testLoadURLWithHTTPHeaders() {
final String url = "http://jb51.net";
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頭信息。
相關(guān)文章
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android SwipeRefreshLayout下拉刷新源碼解析
這篇文章主要為大家詳細解析了Android SwipeRefreshLayout下拉刷新源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android自定義控件實現(xiàn)帶文本與數(shù)字的圓形進度條
這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)帶文本與數(shù)字的圓形進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12android實現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼
這篇文章主要介紹了android實現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼,有需要的朋友可以參考一下2013-12-12