Http web request doesn't maintaining session

2019-05-16 21:07发布

问题:

I have program where i want to scrap some Useful study material for me. This site maintaining session key and some other key also. If I trying to go nested page then it will throw me out and show session out message. I unable to maintain session key in web request class. so please give me some idea that how can i maintain session in web request class.

回答1:

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.



回答2:

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.



回答3:

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