How to simulate http request using WatiN with specific HTTP referrer and query string?

When I use WatiN to go to a specific web page, how can I fake the HTTP referrer with a query string (i.e. request is from google search with query string q=search_term)? So I can verify that the response header has the 301 redirect for specific referrer URL.

I may need to use FiddlerCore to act like a middle man to set the custom referrer but I am not sure how to do that just yet.

I am using ASP.NET with C#.

Thanks!

//WatiN
Browser.GoTo(url);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I needed to do something similar to below:

// session is a custom version of FiddlerCore.Fiddler
// details about BeforeRequest -> http://fiddler.wikidot.com/fiddlercore-demo
session.BeforeRequest += sess =>
    sess.oRequest
        .headers
        .Add(
            "Referer",
            "http://www.i-am-middle-man.com/q=black"
        );

session.BeforeResponse += sess =>
        {
            //sess.oResponse.headers.HTTPResponseCode
            //sess.oResponse.headers["Host"]
        };

var handler = WatiNHandler(BrowserTypes.IE);
handler.GoTo("http://www.my-url.com/");

Method 2

I might be missing the point of what you’re trying to do, but I don’t think WatiN is capable of fakingbthe referrer.

WatiN drives real browsers, so it isn’t in the job of faking stuff. If you want a page to have a specific referrer then you need to have it linked from that referrer.

As you suggest, Fiddler is much better suited for this sort of thing. If you want you can modify requests on the fly using custom rules so that the referrer is whatever you like. You don’t need to use Fiddlercore for this. Running Fiddler as a client proxy, or on you server as a reverse proxy might be enough.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x