4

I've used several HttpWebRequest's in the past but they've all been used to login into a site.

I was wondering how does one make the WebRequest mimic a WebBrowser as in once you're logged in, navigate to a new page, maybe perform an action there, then go to a different page?

I've researched a little about this before and I think it might involve using the prior request's cookies or something.

My question is how do I (I'm assuming) get the cookies from the previous session, then navigate to a page, or complete an action as if we were still on the last request if that makes sense.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
user1017524
  • 241
  • 9
  • 17

1 Answers1

1

the HttpWebRequest has a Cookies property and HttpWebResponse has a CookieContainer property.

you record the cookies from the container, and add them to the next request.

you may also need to set the HTTP referrer header field on the request object.

EDIT :
this will still not get you mimicking a web browser. things like JavaScript will not work/run. and you won't have a DOM to work against.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • No. Its just a strongly typed collection.. Pull up the object browser in VS and take a look at it. It's very straight-forward. – Sam Axe Dec 02 '11 at 22:21
  • @Boo: See http://stackoverflow.com/questions/2972643/how-to-use-cookies-with-httpwebrequest – Jim Mischel Dec 02 '11 at 22:30
  • Meh, it didnt work. I have my first httpwebrequest set up and created the cookie container. And this is my second httpwebrequest but I dont think anything happened: HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create("http://twitter.com/#!/who_to_follow"); httpWebRequest2.CookieContainer = cookieContainer; HttpWebResponse httpWebResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse(); StreamReader stream2 = new StreamReader(httpWebResponse2.GetResponseStream()); – user1017524 Dec 02 '11 at 23:23
  • @user: You've exhausted my knowledge of the subject. I've used `CookieContainer` with some success in the past on other sites, and it seems to have worked. Are you sure that Twitter is sending a cookie? If you want to use the cookies from a previous session, you need to persist that cookie somehow (write it to disk, save it in memory, etc.), and then get it back from that storage before the next request. – Jim Mischel Dec 02 '11 at 23:48