Answered by:
C# Stay logged in while navigating

Question
-
Hello,
I'm having trouble staying connected while navigating with the WebBrowser object.
I figured out how to log in and everything seems to work just fine, but whenever I navigate to another page I get disconnected.
I believe that what I need to do is to keep the cookies, from page to page, but I can't quite figure out how.
Note that I'm using the WebBrowser object to log in and not the HttpWebRequest.
Thanks!
Answers
-
Then it makes no sense that going from one page to the other via the webbrowser doesn't maintain the Login state. If the website you are going to works perfectly using IE9, then putting a webbrowser into your application will do the exact same thing becase that Webbrowser IS the same thing, it is an actual instance of the IE9 webbrower. So in this case you have some "other" problem. Maybe you can explain more.
JP Cowboy Coders Unite!
- Edited by Mr. Javaman II Thursday, March 29, 2012 4:04 PM
- Proposed as answer by Dummy yoyo Friday, March 30, 2012 8:29 AM
- Marked as answer by Dummy yoyo Thursday, April 5, 2012 2:26 AM
All replies
-
There is an object called a CookieContainer which you persist behind the scenes. When you visit a page and you get the cookies, you put them into the 'jar'. When you go out to request a new page, you associate your HttpWebRequest with the cookie jar and then you'll stay logged in.
There's a good example of this here: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer.aspx
-
The CookieContainer appears to only belong to the request. Should I instead keep the CookieCollection from response.Cookies?
Wouldn't the following code return an empty cookie jar?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");
request.CookieContainer = new CookieContainer();
CookieContainer cool = request.CookieContainer;
- Edited by Khailan Wednesday, March 28, 2012 10:26 PM
-
-
-
Then it makes no sense that going from one page to the other via the webbrowser doesn't maintain the Login state. If the website you are going to works perfectly using IE9, then putting a webbrowser into your application will do the exact same thing becase that Webbrowser IS the same thing, it is an actual instance of the IE9 webbrower. So in this case you have some "other" problem. Maybe you can explain more.
JP Cowboy Coders Unite!
- Edited by Mr. Javaman II Thursday, March 29, 2012 4:04 PM
- Proposed as answer by Dummy yoyo Friday, March 30, 2012 8:29 AM
- Marked as answer by Dummy yoyo Thursday, April 5, 2012 2:26 AM
-
May be a moot point, but worth talking about.
The way I've used Cookies with HttpWebRequest was the following: the very first request should not be a post, and just something to get the cookie names. Subsequent requests use the same logic.
CookieContainer cc = new CookieContainer(); HttpWebRequest wrc = (HttpWebRequest) WebRequest.Create(URL); wrc.CookieContainer = cc; wrc.AllowAutoRedirect = false; if (postData.Length > 0) //post data is any form content in the form of controlname=value&controlname2=value2... { wrc.Method = "POST"; wrc.ContentType = "application/x-www-form-urlencoded"; wrc.ContentLength = postData.Length; StreamWriter sr = new StreamWriter(wrc.GetRequestStream()); sr.Write(postData); sr.Flush(); sr.Close(); sr.Dispose(); } HttpWebResponse respc = (HttpWebResponse) wrc.GetResponse(); Response res = new Response(new StreamReader(respc.GetResponseStream()).ReadToEnd(), respc.StatusCode.ToString()); foreach (Cookie cook in respc.Cookies) { if (cook.Domain.StartsWith(".")) { cook.Domain = cook.Domain.Substring(1); } cc.Add(cook); } //Use response text to do whatever iwth the data. res.ResponseText
-
-