HttpWebRequests and logging into websites

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
B

beaker

Nicholas said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top