Scratching my head for a while on this one... This project uses code from "Understand ing HttpWebRequest CookieContainer ?" post on this site.
C#, asp.net 2.0
The following code supplies credentials to the login page properly, if I change the POST credentials, it responds with the invalid login page. On the second get request (after a successful login), it continually redirects to the main page as if the session was over. Here is the code...
Thanks in advance! Any help would be much appreciated.
C#, asp.net 2.0
The following code supplies credentials to the login page properly, if I change the POST credentials, it responds with the invalid login page. On the second get request (after a successful login), it continually redirects to the main page as if the session was over. Here is the code...
Code:
private static string FormLoginGet(string loginUri, string loginData, string requestUri)
{
// cookieContainer is used to store the cookies used by the login
CookieContainer cookieContainer = new CookieContainer();
// First hit the login page
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(loginUri);
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.KeepAlive = false;
req.AllowAutoRedirect = false;
req.ContentType = "application/x-www-form-urlencoded|application/xml";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
// Then grab the content of the desired page
req = (HttpWebRequest)HttpWebRequest.Create(requestUri);
req.AllowAutoRedirect = false;
req.KeepAlive = false;
req.CookieContainer = cookieContainer;
req.Method = "GET";
res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.Found)
{
MessageBox.Show((string)res.Headers["Location"]);
}
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
Comment