Adding request header to cordova webview

To add headers while making request to URL through Cordova WebView following steps has to be followed:

  1. Create a HashMap and add the header parameters to this HashMap
  2. Pass the HashMap to WebView’s loadUrl function along with the URL
  3. In shouldOverrideUrlLoading method of CordovaWebViewClient or WebViewClient add the same HashMap to WebView’s loadUrl function along with the URL

Creating a HashMap:

Map<String, String> map = new HashMap<String, String>();

map.put(“header_name”, “value”);

Passing the header value while calling loadUrl:

loadUrl(url, map);

Passing the same headers in shouldOverrideUrlLoading:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, map);
return true;
}

Note: The HashMap which contains the headers should be passed while calling loadUrl of WebView as well as shouldOverrideUrlLoading of WebViewClient.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s