Pankaj Mishra
Pankaj Mishra

Reputation: 20348

HTTP web request doesn't maintain session

I have a program where I want to scrap some useful study material for personal use. This site maintain a session key and some other key also. If I tried to go to a nested page then it will end the session.

I'm unable to maintain session key using a web request class.

How can I maintain a session using a web request class?

Please help.

Upvotes: 3

Views: 5163

Answers (3)

Josh
Josh

Reputation: 44906

You need to maintain the CookiesCollection across your requests.

var request = (HttpWebRequest)HttpWebRequest.Create("http://www.mysite.com/");
var cookies = new CookieContainer();

//Pass the collection along with each request to maintain session
request.CookieContainer = cookies;

When you get the response back, your container will automatically contain the set of cookies associated with it. You can then reuse that container with subsequent requests.

Upvotes: 9

Sascha
Sascha

Reputation: 10347

Got a similar problem when embedding a Page in an iFrame. Solution is, for security purposes sessions get discarded by browsers (mainly IE8). Maybe this is a splution? Google for P3P to get more info.

-sa

Upvotes: 0

Michael Gattuso
Michael Gattuso

Reputation: 13200

Essentially you will want to read the session cookie from the response header and pass it back in the header with every request you issue. There may be more to it depending on the application you are targeting.

Upvotes: 0

Related Questions