Answered by:
Logging into website, and using cookies to stay logged in.

Question
-
have created a C# application that connects to a my phpBB forum and logs in. I am now trying to use a HttpWebRequest to go to a page and grab the page as a string. However I keep getting logged out. How do I use the cookie created so I can stay logged in and do what I need it to do?
What I have so far...
public static CookieContainer login(string url, string username, string password, Form1 form) { if (url.Length == 0 || username.Length == 0 || password.Length == 0) { Console.WriteLine("Information missing"); return null; } CookieContainer myContainer = new CookieContainer(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = myContainer; // Set type to POST request.Method = "POST"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; request.ContentType = "application/x-www-form-urlencoded"; // Build the new header, this isn't a multipart/form, so it's very simple StringBuilder data = new StringBuilder(); data.Append("username=" + Uri.EscapeDataString(username)); data.Append("&password=" + Uri.EscapeDataString(password)); data.Append("&login=Login"); // Create a byte array of the data we want to send byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); // Set the content length in the request headers request.ContentLength = byteData.Length; Stream postStream; try { postStream = request.GetRequestStream(); } catch (Exception e) { Console.WriteLine("Login - " + e.Message.ToString() + " (GRS)"); return null; } // Write data postStream.Write(byteData, 0, byteData.Length); HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (Exception e) { Console.WriteLine("Login - " + e.Message.ToString() + " (GR)"); return null; } bool isLoggedIn = false; // Store the cookies foreach (Cookie c in response.Cookies) { if (c.Name.Contains("_u")) { if (Convert.ToInt32(c.Value) > 1) { isLoggedIn = true; } } myContainer.Add(c); } if (isLoggedIn) { string _url = "http://www.dandrews.net/forum/custom.php"; string strResult = ""; HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(_url); _request.CookieContainer = myContainer; HttpWebResponse _response = (HttpWebResponse)_request.GetResponse(); using (StreamReader sr = new StreamReader(_response.GetResponseStream())) { strResult = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } form.userbox.Text = strResult; return myContainer; } else { return null; } }
Answers
-
1. How do you know that you are realy logged in?
Did you check it?
2. In order to move from one page to another and keeping yourself logged in,
you'll need to use CookieContainer.
http://msdn.microsoft.com/en-us/library/system.net.cookiecontainer.aspx
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...- Proposed as answer by Noam B Sunday, January 1, 2012 12:19 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, January 11, 2012 3:00 AM
All replies
-
Hi,
As this question is related to ASP.NET, there is a dedicated forum for that. So, I suggest you to ask this question in ASP.NET Forum.
Best Wishes.
Please mark this post as answer if it solved your problem. Happy Programming! -
1. How do you know that you are realy logged in?
Did you check it?
2. In order to move from one page to another and keeping yourself logged in,
you'll need to use CookieContainer.
http://msdn.microsoft.com/en-us/library/system.net.cookiecontainer.aspx
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...- Proposed as answer by Noam B Sunday, January 1, 2012 12:19 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, January 11, 2012 3:00 AM
-
Hi,
Any update of this issue? If you need any further assistance, please feel free to let me know.
Happy New Year!
Michael Sun [MSFT]
MSDN Community Support | Feedback to us