技术小黑屋

Load URL With Extra HTTP Headers Using WebView in Android

As we know, Webview will add the referer into the reqeust if we click a hyperlink to navigate to another one. But if we want to add a referer when a hard loading starts, What can we do to make it?

Since Froyo(API Level 8), the webview starts providing an interface for us to send extra HTTP headers when loading a URL.

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.

Now this is a working example.

1
2
3
4
5
6
7
public void testLoadURLWithHTTPHeaders() {
    final String url = "http://androidyue.github.io/";
    WebView webView = new WebView(getActivity());
    Map<String,String> extraHeaders = new HashMap<String, String>();
    extraHeaders.put("Referer", "http://www.google.com");
    webView.loadUrl(url, extraHeaders);
}

For more details about HTTP Headers, please visit List_of_HTTP_header_fields More details about Webview, please visit http://developer.android.com/reference/android/webkit/WebView.html

Others

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2t07a740wrok0


我的知乎 Live 推荐