471,306 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

HttpWebRequests and logging into websites



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
Jan 10 '06 #1
3 2014
beaker,

The cookie issue is most likely it. HTTP is a stateless protocol. What
you have to do on the HttpWebRequest is to use the CookieContainer property
to set the cookies for the request.

The thing is, once you log in, you will receive cookies back (and there
could possibly be one on your system already from the site, which you might
have to send). You have to take the cookies from the response and feed them
into the next request.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"beaker" <ra**************@rhubarbblahblah.net> wrote in message
news:ZM********************@fe3.news.blueyonder.co .uk...


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

Jan 10 '06 #2
Nicholas Paldino [.NET/C# MVP] wrote:
beaker,

The cookie issue is most likely it. HTTP is a stateless protocol. What
you have to do on the HttpWebRequest is to use the CookieContainer property
to set the cookies for the request.

The thing is, once you log in, you will receive cookies back (and there
could possibly be one on your system already from the site, which you might
have to send). You have to take the cookies from the response and feed them
into the next request.

Hope this helps.


Thanks.

If I'm reading you right, are you suggesting

* create CookieContainer for the initial request
* save and use the cookies (returned after initial request) for further
requests?

I've just tried that, but I'm not receiving any cookies back in the
response....

(You were right, there is already a cookie my Cookies folder, but I
don't want to use this as hopefully I can share this app with other people).

Thanks.
Jan 10 '06 #3
beaker,

Well, you can call WinInet API functions through the P/Invoke layer to
get the information about that cookie if you wanted. This would allow you
to distribute your app as well.

The thing to understand about web apps is that there are a good number
of variables that it can use to process the request and produce a result.
Usually, it can be something like cookies, but it can also be other things,
like the referrer field, the user agent, etc, etc.

You might want to get a sniffer (such as Fiddler, which is a free tool
from MS, I believe), which you can use to sniff the requests being sent to
the website. This way, you can tailor the HttpWebRequest to do what you
want.

Also, you should be using more using statements to dispose of items in
your code correctly (streams, readers, the response).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"beaker" <ra**************@rhubarbblahblah.net> wrote in message
news:iC********************@fe1.news.blueyonder.co .uk...
Nicholas Paldino [.NET/C# MVP] wrote:
beaker,

The cookie issue is most likely it. HTTP is a stateless protocol.
What you have to do on the HttpWebRequest is to use the CookieContainer
property to set the cookies for the request.

The thing is, once you log in, you will receive cookies back (and
there could possibly be one on your system already from the site, which
you might have to send). You have to take the cookies from the response
and feed them into the next request.

Hope this helps.


Thanks.

If I'm reading you right, are you suggesting

* create CookieContainer for the initial request
* save and use the cookies (returned after initial request) for further
requests?

I've just tried that, but I'm not receiving any cookies back in the
response....

(You were right, there is already a cookie my Cookies folder, but I don't
want to use this as hopefully I can share this app with other people).

Thanks.

Jan 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Neil Benn | last post: by
1 post views Thread by jjesso | last post: by
2 posts views Thread by Tor Erik Sønvisen | last post: by
6 posts views Thread by pmatos | last post: by
23 posts views Thread by Rotem | last post: by
3 posts views Thread by philwilks | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.