9

I need to display a web page in my Android app which is looking for a referer to bypass the security. I'm new to Android so I know how to display the web page in a web view but not how to send the 'referer' along with the url request. I'm sure it will need to update the HTTPHeaderField but I cannot find any reference for it in Android. The code below is what I'm using to bring up the web page but without the 'referer' it says 'Access Denied'

WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://www.mywebsite.com");

I think the answer may lie in the WebView.LoadURL method which adds extra headers but I can't find any examples of it.

user616076
  • 3,907
  • 8
  • 38
  • 64

2 Answers2

19

For which API-level do you need that function?

Since API Level 8 there is a second loadUrl function:

  public void loadUrl (String url, Map<String, String> extraHeaders)

With the extraHeaders you should be able to send a referrer.


EDIT:

Here is a complete working example:

  String url = "http://www.targetserver.tld/";

  Map<String, String> extraHeaders = new HashMap<String, String>();
  extraHeaders.put("Referer", "http://www.referer.tld/login.html");

  WebView wv;
  wv = (WebView) findViewById(R.id.webview);
  wv.loadUrl(url, extraHeaders);
Davit Huroyan
  • 302
  • 4
  • 16
MacGucky
  • 2,494
  • 17
  • 17
  • Thanks for the reply, I did notice this in the Android reference but was hoping to find an example. My referer is "myblog201.com/" – user616076 Mar 17 '11 at 17:05
  • Thanks for the example, I copied the code to try it but I get the following error 'The method loadUrl(String) in the type WebView is not applicable for the arguments (String, Map)' and it prompts me to remove the extra argument. Am I doing something wrong? – user616076 Mar 18 '11 at 07:26
  • What API Level you are using? As I wrote, the new loadUrl-function with the second parameter exists since API Level 8. – MacGucky Mar 18 '11 at 08:02
  • I did notice that and checked my AndroidManifest.xml, the setting is now but was originally 7 although changing it doesn't seem to have made any difference, as it is still flagging the error. – user616076 Mar 18 '11 at 09:12
  • Sorry, have just been into Project>Properties>Android>Project Build Traget and changed the API Level to 8 and that has done the trick. – user616076 Mar 18 '11 at 09:31
  • As of KitKat this no longer seems to work on device. I used this solution for a long time and recently got a complaint. I debugged with Charles Proxy and KitKat seems to discard the "Referer" header. Name it anything else like "Referrer" and the header is allowed through but that doesn't really help unfortunately. If anyone has a suggestion for an alternative solution I'd love to hear it! – ToddH Sep 05 '14 at 23:36
  • @ToddH I am able to reproduce the same issue in Kitkat. But this behavior is different wrt to different manufactureres. For eg S4 with Kitkat passes the Referer but a Motorola G does not. Have you found a solution to this issue or a workaround maybe? – SoH Jan 21 '15 at 11:56
0

You will need to use Intent Filters to capture and modify WebView requests.

Assuming that you need to specify doamin.com/page.html as referrer

  1. Setup intent filters to capture all http requests in WebView
  2. If request is for "doamin.com/page.html", return pre-defined page that has a refresh tag to send user to "http://www.mywebsite.com"
  3. domain.com/page.html would be sent as a referrer to mywebsite.com

In newer APIs you can specify headers in loadUrl itself.

Shamit Verma
  • 3,839
  • 23
  • 22