c# - HttpWebRequest and HttpWebResponse : maintaining state of logged in request for consecutive queries -


I have some HttpWebRequances and HttpWebResponses chained together, it's also using cookies container.

The code simulates a user is going through three different 'I agree' pages which set the cookie information, logging in with the username and password on the fourth, and in the form of a string Do a search (search) at the fifth place of feedback.

Is there a way I can keep the HTTP WebRequest object as 'logged in' to avoid logging a user every time as 'log in'?

Can I set it as static, and if its empty or missing cookie information can run through all the steps, otherwise just post the user is required? What is a good pattern for this?

If you are logging in to use cookie-based authentication, you get a System.Net. CookieContainer will create where you store the authentication cookie. It is very simple:

  Cookie Container Container = New Cookie Container (); // Create a web request request Cookie Sensitive = Container; // Create the next web request next.Request.CookieContainer = Container; // More ...  

Just reuse the cookie contributor object for all your HttpWebRequest objects, and keep it in memory for later use.

Cookie Container is serializable, so that you can continue it on disk if you need it so that you can also keep your own cookies, even if your user will restart your application.

Alternatively, if the page does not use cookies, but stores the session ID in the URL, then you need to keep it with the session ID in the URL of the pages you go to. Pass it simply add it to the URL and it should work. : -)


Comments