By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

Login into a web page

P: n/a
Hi,

I am using the HttpWebRequest and HttpWebResponse classes to pull
information from a web server on the internet.

I have an account on one of the webservers that I need to log into
programatically. The connection is not secure (not https, etc.). The login
page just has simple name & password textboxes on it.

Can someone please tell me how I can programatically login using the
HttpWebXXX classes.

Thanks!

-TJ
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> I am using the HttpWebRequest and HttpWebResponse classes to pull
information from a web server on the internet.

I have an account on one of the webservers that I need to log into
programatically. The connection is not secure (not https, etc.). The
login page just has simple name & password textboxes on it.

Can someone please tell me how I can programatically login using the
HttpWebXXX classes.


This depends on how the login page is designed. If it is a ASP.NET forms
authentication page, you'll have to do a post back.
The easiest approach is to use the System.Net.WebClient class. As an
example, imagine this is the HTML code for the login page:
<form name="Form1" method="post" action="login.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDw1MzgxO2w8X2N0bDA6X2N0bDA6X2N0bDA6X2N0bDc 7Pj4" />
<P>Username: <input name="Username" type="text" id="Username" /></P>
<P>Password: <input name="Password" type="text" id="Password" /></P>
<P><input type="submit" name="Login" value="Login" id="Login" /></P>
</form>

The HTTP Request body is made up of ampersand separated key / value pairs.
The request body for the above example will look like this:
Username=Anders&Password=P@ssword&Login=Login

This body has to be posted back to the page.

WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string
requestBody=viewstate+"Username=Anders&Password=P@ ssword&Login=Login";
byte[] response = webClient.UploadData("http://andersnoras.info/login.aspx",
"POST", Encoding.ASCII.GetBytes(requestBody));

The viewstate variable should include the value of the hidden INPUT field
called __VIEWSTATE. You'll need to extract this value from the initial
request.

If you need to accept cookies from the page, you'll have to use the
HttpWebRequest class:

CookieContainer cookies = new CookieContainer();
webRequest = WebRequest.Create("http://andersnoras.info/login.aspx") as
HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
StreamWriter requestWriter = new
StreamWriter(webRequest.GetRequestStream());
string
requestBody=viewstate+"Username=Anders&Password=P@ ssword&Login=Login";
requestWriter.Write(requestBody);
requestWriter.Close();
webRequest.GetResponse().Close();

Anders Norås
http://dotnetjunkies.com/weblog/anoras
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.