Logo
My Account |  Site Map | Contact Us  
Welcome Guest Search | Active Topics | Sign In | Register

Request Referer Header bug Options
Zius
Posted: Thursday, August 9, 2018 11:42:16 AM
Rank: Newbie
Groups: Member

Joined: 7/14/2018
Posts: 7
few weeks ago i had a problem trying to set the Referer header on a request i made a post and was instructed to upgrade to the latest version which i did. i was able to set the Referer header but i was just testing so i was using the following code:

Code: C#
e.Request.Headers.Add("Referer","http://example.com");

here is the link for previous post: https://www.essentialobjects.com/forum/postst10970_Request-Referer-Header-Not-Working.aspx

everything was working fine. but the app im working one needs to change the Referer header on each unique request with a different URL that is being set dynamically before i load the request .so i get the URL from an object Property
declaring the code like this is not working and causes the webview to crash

Code: C#
public class RefererUrl{

public string Url{get;set;}
}


before each request the Url Property is set to a different URL example:

Code: C#
RefererUrl Ref = new RefererUrl();
Ref.Url = "https://facebook.com";


then i try to set the Referer header for the request like so:

Code: C#
Request req= new Request("http://mytargeturl.com");
  req.Headers.Add("Referer",Ref.Url);
  //or 
  req.Headers["Referer"] =Ref.Url;
  //then call the loadrequest method
  WebView1.LoadRequest(req);

or under the BeforeRequestLoad
Code: C#
private void OnBeforeRequestLoad(object sender, BeforeRequestLoadEventArgs e)
        { 
           e.Request.Headers["Referer"] =Ref.Url;
           
           
        }



this either cause the webview to crash or the request is loaded but without a Referer Header. and it not the case when i set any other header. if i set a header called 'Test' for example its working fine wither im using a literal string or a string variable.
which is a strange behavior im still using a string value when i call Header.Add() !!!
so is this a bug or you only allow for literal string values in Headers!!?


eo_support
Posted: Thursday, August 9, 2018 4:12:55 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 20,821
Hi,

We tested both of the following cases with our TabbedBrowser sample application and it appears to work fine:

1. Handle BeforeRequestLoad event and then call e.Headers["Referer"] = some_url inside the event handler;

2. Create a separate Request object, setting Referer the same way and call webView.LoadRequest to load the request;

In both case Referer is sent to the server correctly.

There are a couple of things that we will need more information from you:

1. We can not see any substantial difference between your scenario and our test scenarios. How you come to your Referal value is up to you and that should not change anything. So the fact that you have a RefererUrl class should be irrelevant;

2. You mentioned that the WebView crashes. We do not see any crashes here. So we will need you to send a test app to us demonstrating the problem and we will be happy to investigate further. See here for more details about how to send test app to us:

https://www.essentialobjects.com/forum/test_project.aspx

Internally the Referer header is indeed handled significantly differently than other headers because the browser engine will supply a referrer value by default and unless you have explicitly supplied yours to override the internal value, the automatically derived internal value will be used. However this should not alter the logic mentioned above.

Thanks!
Zius
Posted: Thursday, August 9, 2018 4:49:50 PM
Rank: Newbie
Groups: Member

Joined: 7/14/2018
Posts: 7
eo_support wrote:
Hi,
There are a couple of things that we will need more information from you:

1. We can not see any substantial difference between your scenario and our test scenarios. How you come to your Referal value is up to you and that should not change anything. So the fact that you have a RefererUrl class should be irrelevant;

Thanks!

Thanks for the quick reply,
However the issue arise when i use point 1. if i declare the variable inside BeforeRequestLoad either by making a variable first or by passing the string to the Request.Header.Add("Referer",variable_name) method or e.Header['Referer'] = variable_name like so

Code: C#
private void OnBeforeRequestLoad(object sender, BeforeRequestLoadEventArgs e)
        { 
           string refererUrl = "http://someurl.com"
           e.Request.Headers["Referer"] =refererUrl ;
           //or like this
           e.Headers.Add("Referer",refererUrl );         
       }

it works fine. but if i set the variable on a different object or even as private field on the same class where i instantiate an instance of the Webview i get the crash or the behavior where the header is not being set and sent.
this is why it drove me a bit crazy because its basically just passing a string to a method.

could you please try with the scenario i described. i will still provide a test app.
Zius
Posted: Thursday, August 9, 2018 5:55:06 PM
Rank: Newbie
Groups: Member

Joined: 7/14/2018
Posts: 7
ok i have noticed something just now. i want to add the Referer header on the first get request where there isn't a referer header is set yet by the browser
,Example:
i want to make the first get request to http://example.com has a referer header set to it. in my case i was using a condition to only set the header once on the first request but now i removed this condition and i can see they header is being set.
so how can i set a Referer header on the first request and force the browser to add it if it's not there? the Headers.Add() method not doing it.
Thanks again.
eo_support
Posted: Thursday, August 9, 2018 6:03:57 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 20,821
Hi,

There is nothing particular about the first request. You can do it in either way mentioned above. For example, you can do it in BeforeRequestLoad handler and use e.Request.Headers["Referer"] = your_referal_url. Or you can use WebView.LoadRequest to load the first Url you want to load and set Referer on the Request object you passed to LoadRequest.

I do not understand what you meant by "if i set the variable on a different object or even as private field on the same class where i instantiate an instance of the Webview i get the crash or the behavior where the header is not being set and sent". That may not be directly related to Referer. In any case if you send us a test app we will be happy to look into it.

Thanks!
Zius
Posted: Thursday, August 9, 2018 8:45:35 PM
Rank: Newbie
Groups: Member

Joined: 7/14/2018
Posts: 7
eo_support wrote:
Hi,
I do not understand what you meant by "if i set the variable on a different object or even as private field on the same class where i instantiate an instance of the Webview i get the crash or the behavior where the header is not being set and sent". That may not be directly related to Referer. In any case if you send us a test app we will be happy to look into it.
Thanks!

No need for the test app. turns out if you visit a URL with HTTP:// suffix while you are coming from a URL with HTTPS:// suffix the web browser removes the Referer header for security reasons!!
so this explains the weird behaviors..... sigh so much time was wasted just because of something like this :)
Thanks again for your patience and great support and sorry for any inconvenience i might caused.
eo_support
Posted: Friday, August 10, 2018 10:42:09 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 20,821
Glad to hear that! Yes that makes perfect sense. Please do not hesitate to contact us in the future if you run into anything else.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.