B
beaker
Hello,
I'm trying to write an app which will periodically log in to a game
website so I can check for changes to some of my player info. The code
I've come up with so far is below, and, as you might have guessed,
doesn't quite work - what I get back is the raw HTML of the login page,
rather than the page I should get after succesfully logging in.
Couple of questions really, am I going in the right direction, and what
do I need to do to make it work?
NB There's also a warning on the login page that if it fails, it is
likely to be with cookies not being enabled, I'm not even really sure
what to do with cookies - this is my first foray into web stuff with C#.
Thanks.
Code:
=====
The form on the login page:
<form method=post action="LoginCheck">
<input type="text" name="name" size="20" maxlength="20">
<input type="password" name="password" size="20" maxlength="20">
<input type="submit" value="CONTINUE">
</form>
My code to try to login (m_name, m_password, m_uriLogin all set elsewhere):
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "name=" + m_name + "&password=" + m_password;
byte[] data = encoding.GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_uriLogin);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Flush();
newStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream istrm = resp.GetResponseStream();
StreamReader sr = new StreamReader(istrm);
TxtReport.AppendText(sr.ReadToEnd()); // TxtReport is a text box
I'm trying to write an app which will periodically log in to a game
website so I can check for changes to some of my player info. The code
I've come up with so far is below, and, as you might have guessed,
doesn't quite work - what I get back is the raw HTML of the login page,
rather than the page I should get after succesfully logging in.
Couple of questions really, am I going in the right direction, and what
do I need to do to make it work?
NB There's also a warning on the login page that if it fails, it is
likely to be with cookies not being enabled, I'm not even really sure
what to do with cookies - this is my first foray into web stuff with C#.
Thanks.
Code:
=====
The form on the login page:
<form method=post action="LoginCheck">
<input type="text" name="name" size="20" maxlength="20">
<input type="password" name="password" size="20" maxlength="20">
<input type="submit" value="CONTINUE">
</form>
My code to try to login (m_name, m_password, m_uriLogin all set elsewhere):
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "name=" + m_name + "&password=" + m_password;
byte[] data = encoding.GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_uriLogin);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Flush();
newStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream istrm = resp.GetResponseStream();
StreamReader sr = new StreamReader(istrm);
TxtReport.AppendText(sr.ReadToEnd()); // TxtReport is a text box