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

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px; line-height:17px; text-align:left 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?
<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px; line-height:17px; text-align:left What I have so far...

<span style="font-family:Arial,Liberation Sans,DejaVu Sans,sans-serif; font-size:14px; line-height:17px; text-align:left
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; static CookieContainer login(<span style="color:Blue; string url, <span style="color:Blue; string username, <span style="color:Blue; string password, Form1 form)
{
<span style="color:Blue; if (url.Length == 0 || username.Length == 0 || password.Length == 0)
{
Console.WriteLine(<span style="color:#A31515; "Information missing");
<span style="color:Blue; return <span style="color:Blue; null;
}

CookieContainer myContainer = <span style="color:Blue; new CookieContainer();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = myContainer;

<span style="color:Green; // Set type to POST
request.Method = <span style="color:#A31515; "POST";
request.UserAgent = <span style="color:#A31515; "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 = <span style="color:#A31515; "application/x-www-form-urlencoded";

<span style="color:Green; // Build the new header, this isnt a multipart/form, so its very simple
StringBuilder data = <span style="color:Blue; new StringBuilder();
data.Append(<span style="color:#A31515; "username=" + Uri.EscapeDataString(username));
data.Append(<span style="color:#A31515; "&password=" + Uri.EscapeDataString(password));
data.Append(<span style="color:#A31515; "&login=Login");

<span style="color:Green; // Create a byte array of the data we want to send
<span style="color:Blue; byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

<span style="color:Green; // Set the content length in the request headers
request.ContentLength = byteData.Length;

Stream postStream;
<span style="color:Blue; try
{
postStream = request.GetRequestStream();
}
<span style="color:Blue; catch (Exception e)
{
Console.WriteLine(<span style="color:#A31515; "Login - " + e.Message.ToString() + <span style="color:#A31515; " (GRS)");
<span style="color:Blue; return <span style="color:Blue; null;
}

<span style="color:Green; // Write data
postStream.Write(byteData, 0, byteData.Length);

HttpWebResponse response;
<span style="color:Blue; try
{
response = (HttpWebResponse)request.GetResponse();
}
<span style="color:Blue; catch (Exception e)
{
Console.WriteLine(<span style="color:#A31515; "Login - " + e.Message.ToString() + <span style="color:#A31515; " (GR)");
<span style="color:Blue; return <span style="color:Blue; null;
}

<span style="color:Blue; bool isLoggedIn = <span style="color:Blue; false;

<span style="color:Green; // Store the cookies
<span style="color:Blue; foreach (Cookie c <span style="color:Blue; in response.Cookies)
{
<span style="color:Blue; if (c.Name.Contains(<span style="color:#A31515; "_u"))
{
<span style="color:Blue; if (Convert.ToInt32(c.Value) > 1)
{
isLoggedIn = <span style="color:Blue; true;

}
}
myContainer.Add(c);
}

<span style="color:Blue; if (isLoggedIn)
{

<span style="color:Blue; string _url = <span style="color:#A31515; "http://www.dandrews.net/forum/custom.php";
<span style="color:Blue; string strResult = <span style="color:#A31515; "";

HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(_url);
_request.CookieContainer = myContainer;
HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();



<span style="color:Blue; using (StreamReader sr = <span style="color:Blue; new StreamReader(_response.GetResponseStream()))
{
strResult = sr.ReadToEnd();
<span style="color:Green; // Close and clean up the StreamReader
sr.Close();
}
form.userbox.Text = strResult;

<span style="color:Blue; return myContainer;

}
<span style="color:Blue; else
{
<span style="color:Blue; return <span style="color:Blue; null;
}
}

[/code]
<br/>
<br/>



View the full article
 
Back
Top