As we all know, when you click on a hyperlink to jump, WebView will automatically send the current address as referer (referral) to the server, so many server-side programs through whether to contain referer to control hotlinking, so sometimes, directly enter a network address, there may be problems, Then how to solve the hotlinking control problem, in fact, in the webview load when adding a referer can be, how to add it?
Starting with the Android 2.2 (API 8), WebView has added an interface method that allows us to send additional HTTP header information when we load the page.
Copy Code code as follows:
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 is used in the HTTP request for this URL, specified as a map from name to Value. Note So if this is map contains any of the headers which are set by default through this webview, such as those controlling Ng, accept types or the user-agent, their values may is overriden by this webview ' s defaults.
Here's a simple demo to show how to use the following.
Copy Code code as follows:
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);
}
The same can be applied to other HTTP header information such as useragent.