Qbunia
Qbunia

Reputation: 1933

How to Programmatically Log in to a Website

I dont know how to programmatically login to this site I've searched through stackoverflow and found this, but I still don't know what to put into URL or URI.

Upvotes: 0

Views: 2975

Answers (1)

Paul Tyng
Paul Tyng

Reputation: 7584

When I just type in username 'abc' and password 'def' and hit the button I get the following post data:

next=apps%2Flinks%2F&why=pw&email=abc&password=def&fw_human=

So that leads me to beleive if you just use that post data and replace it with the appropriate information, you can simulate a manual login.

So from the stack overflow you linked, this would take the form:

string formUrl = "http://ratings-plus.webs.com/apps/auth/doLogin"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("next=apps%2Flinks%2F&why=pw&email={0}&password={1}&fw_human=", "your email", "your password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
    os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];

Note that in future requests you will need to then use whatever the cookie value that is returned to maintain your authenticated status.

Upvotes: 3

Related Questions