473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Login into a web page

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
1 7853
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Simon | last post by:
Hi, I have a Login.php page that logs the user in and out. I has two forms within the page, (depending on what we are trying to do), either one to log in or out. The form calls itself using a...
9
by: buran | last post by:
Dear ASP.NET Programmers, How can I post data to an ASP.NET login page and pass authentication? The login page uses forms authentication, users must supply usernames and password and have to...
4
by: nicholas | last post by:
Hi, Got an asp.net application and I use the "forms" authentication mode defined in the web.config file. Everything works fine. But now I would like to add a second, different login page for...
2
by: pv | last post by:
Hi everyone, I need help with following scenario, please: Users are accessing same web server from intranet (users previously authenticated in Active Dir) and from extranet (common public...
7
by: Alan Silver | last post by:
Hello, Sorry this is a bit wordy, but it's a pretty simple question... I have a web site, http://domain/ which is a public site, part of which (http://domain/a/) is protected by forms...
0
by: muder | last post by:
I have a standard Login ASP.NET 2.0 control on a login Page, a LoginName and LoginStatus controls on the member's page. once the user login successfully I am redirecting the user to Member.aspx...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
3
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
10
by: DavidPr | last post by:
When I logout as one user and log in under a different user, it opens with the last user's information. User 1 - Unsername: Davey Jones User 2 - Unsername: David Smith I log out from Davey...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.