As we all know, when you click on a hyperlink to jump, the WebView will automatically send the current address to the server as a referer (referrer). Therefore, many server-side programs control link theft by whether they include a referer. So sometimes, if you directly enter a network address, there may be problems. So how to solve the problem of link theft control? In fact, just add a referer when the webview is loaded. How to add it?
Starting from Android 2.2 (that is, API 8), WebView has added a new interface method, which is to facilitate us to send other HTTP headers when loading web pages.
Copy the codeThe code is 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 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.
Here is a simple demo to show how to use it.
Copy the codeThe code is as follows:
public void testLoadURLWithHTTPHeaders() {
final String url = "";
WebView webView = new WebView(getActivity());
Map<String,String> extraHeaders = new HashMap<String, String>();
("Referer", "");
(url, extraHeaders);
}
The above can also be applied to other HTTP header information such as UserAgent.