SoFunction
Updated on 2025-04-06

Detailed explanation of the sending HTTP header information when Android Webview loads a webpage

Detailed explanation of the sending HTTP header information when Android Webview loads a webpage

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.

Here is a simple demo to show how to use it.

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 UserAgent and other HTTP header information.

Thank you for reading, I hope it can help you. Thank you for your support for this site!