Android中Webview打開網(wǎng)頁的同時(shí)發(fā)送HTTP頭信息方法
眾所周知,當(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://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頭信息。
- Android WebView打開網(wǎng)頁一片空白的解決方法
- Android開發(fā)筆記之如何正確獲取WebView的網(wǎng)頁Title
- Android WebView實(shí)現(xiàn)網(wǎng)頁滾動截圖
- 詳解android 用webview加載網(wǎng)頁(https和http)
- Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解
- Android編程實(shí)現(xiàn)webview將網(wǎng)頁打包成apk的方法
- Android中替換WebView加載網(wǎng)頁失敗時(shí)的頁面
- Android webView如何輸出自定義網(wǎng)頁
相關(guān)文章
Android仿Iphone屏幕底部彈出半透明PopupWindow效果
這篇文章主要為大家詳細(xì)介紹了Android仿Iphone屏幕底部彈出半透明PopupWindow效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android SwipeRefreshLayout下拉刷新源碼解析
這篇文章主要為大家詳細(xì)解析了Android SwipeRefreshLayout下拉刷新源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android自定義控件實(shí)現(xiàn)帶文本與數(shù)字的圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)帶文本與數(shù)字的圓形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android實(shí)現(xiàn)左滑關(guān)閉窗口
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左滑關(guān)閉窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04android實(shí)現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼
這篇文章主要介紹了android實(shí)現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼,有需要的朋友可以參考一下2013-12-12