Android webview to open Web page while sending HTTP header information method _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.