Android中Webview打開網(wǎng)頁的同時發(fā)送HTTP頭信息方法
眾所周知,當你點擊一個超鏈接進行跳轉(zhuǎn)時,WebView會自動將當前地址作為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自定義日歷Calender代碼實現(xiàn)
這篇文章主要為大家詳細介紹了Android自定義日歷Calender實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android基于高德地圖完全自定義Marker的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android基于高德地圖完全自定義Marker的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07
Android ListView滾動到底后自動加載數(shù)據(jù)
這篇文章主要為大家詳細介紹了Android之ListView滾動到底后自動加載數(shù)據(jù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法
這篇文章主要介紹了Android編程向服務(wù)器發(fā)送請求時出現(xiàn)中文亂碼問題的解決方法,實例分析了Android參數(shù)傳遞過程中中文亂碼的解決技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11

