473,757 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7917
> 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.WebC lient class. As an
example, imagine this is the HTML code for the login page:
<form name="Form1" method="post" action="login.a spx" id="Form1">
<input type="hidden" name="__VIEWSTA TE"
value="dDw1Mzgx O2w8X2N0bDA6X2N 0bDA6X2N0bDA6X2 N0bDc7Pj4" />
<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@ssw ord&Login=Login

This body has to be posted back to the page.

WebClient webClient = new WebClient();
webClient.Heade rs.Add("Content-Type", "applicatio n/x-www-form-urlencoded");
string
requestBody=vie wstate+"Usernam e=Anders&Passwo rd=P@ssword&Log in=Login";
byte[] response = webClient.Uploa dData("http://andersnoras.inf o/login.aspx",
"POST", Encoding.ASCII. GetBytes(reques tBody));

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.Crea te("http://andersnoras.inf o/login.aspx") as
HttpWebRequest;
webRequest.Meth od = "POST";
webRequest.Cont entType = "applicatio n/x-www-form-urlencoded";
webRequest.Cook ieContainer = cookies;
StreamWriter requestWriter = new
StreamWriter(we bRequest.GetReq uestStream());
string
requestBody=vie wstate+"Usernam e=Anders&Passwo rd=P@ssword&Log in=Login";
requestWriter.W rite(requestBod y);
requestWriter.C lose();
webRequest.GetR esponse().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
2971
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 post method and either logs the user in our out given the information from the form. but every pages use sessions and cookies, if the user is successfully logged in then the cookies and session values are updated, (as well as MySQL).
9
3587
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 click on a submit button. I want to automate this process by supplying values with HttpWebRequest and then download a file on the site. I think that I cannot invoke the submit button. Pleeeasee help, thanks in advance
4
2829
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 the users that go in a specific folder. How can I do this?
2
2908
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 users). If user is from intranet, web server should recognize it and application should create additional options in controls regarding groups the user belongs to. If user is from extranet it should be logged in as anonymous and a link to login page...
7
2452
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 authentication. I would like to configure it so that anyone not logged in, trying to access the protected part will not be redirected to the login page, but
0
5275
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 page. The following is my machine configuration Windows XP Pro Service Pack2 IIS 5.1 SQL Server 2000 visual Studio 2005 DISABLE ANONMYOUS ACCESS IN IIS ENABLE WINDOWS AUTHENTICATION
6
3359
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 the past in other places, and while the help was much appreciated, it seemed everyone just wanted to 'theoretically' explain how to do it, but when I tried to do it myself, I couldn't login. I want to simply pass the email address and password to...
3
6224
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 has to tell then them its wrong information but currently it takes then to a next page and then tells them its incorrect information. This is tedious as every time they enter wrong they will be redirected to a different page and then they have to...
10
4819
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 Jones, then login as David Smith the Welcome message below will show "Welcome Davey". And it will be Davey's information that is accessible - not David Smith's. So something is amiss but I don't know what. (BTW, this login script is based on the...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.