C # website logon Study Notes (1): log on to a simple website

Source: Internet
Author: User
Recently, I saw several articles written by deep blue in the garden about website login. Article I thought it was quite fun and interesting, so I decided to study it. Whenever there is a gradual learning process, the following are the initial learning steps:
1. Create a simple website locally and try to log in with C;
2. Find a website on the Internet that can log on without a verification code and try to log on with C;
3. Find a website on the Internet that requires a verification code to log on, and try to log on with C #

1. Create the simplest test website: This website only contains two pages: default. aspx (logon page) and loginsuccess. asp (logon success page ). When a user successfully logs on to the default. aspx page, the system jumps to the logon success page. The logon success page can only be accessed after logon. The website uses session to save and determine whether the user is logged on.

Ii. packet capture Analysis: Capture packets to get a glimpse of the data required to log on to the website. Here, we use HTTP analyzer to capture packets. When capturing packets, we should disable other software that may access the network to reduce the impact of other useless packages on data analysis.
1. enable HTTP analyzer and start packet capture;
2. Access the logon page through a browser (http: // localhost/testlogin/default. aspx );
3. Enter the user name and password to log on to the website. (After logon, the system automatically redirects to the logon success page http: // localhost/testlogin/loginsuccess. aspx)

(Figure 1)

(Figure 2)

3. compile C #ProgramAchieve website login
1. First, let's take a look at how to use httpwebrequest, httpwebresponse, and stream to access general pages. Code OK: Public   String Gethtml ( String URL)
{
Httpwebrequest;

Httpwebrequest = (Httpwebrequest) httpwebrequest. Create (URL); // URL = "http: // localhost/testlogin/default. aspx"

Httpwebresponse;
Httpwebresponse = (Httpwebresponse) httpwebrequest. getresponse ();
Stream responsestream = Httpwebresponse. getresponsestream ();
Streamreader =   New Streamreader (responsestream, encoding. utf8 );
String Html = Streamreader. readtoend ();
Streamreader. Close ();
Responsestream. Close ();

Return HTML;
}

2. If postdata is contained, you must specify the relevant attributes of httpwebrequest. These attribute values can be obtained from the packet capture: Public   String Gethtml ( String URL, String Postdata, method)
{
Byte[] Byterequest=Encoding. Default. getbytes (postdata ); // Postdata =
"_ Viewstate = % response & txtusername = A & txtpassword = B & btnlogin = login & __ eventvalidation = % response % 2fbt6koxv5qmx7k4egfwj1si0"

Httpwebrequest;
Httpwebrequest = (Httpwebrequest) httpwebrequest. Create (URL); // URL = "http: // localhost/testlogin/default. aspx"

Httpwebrequest. contenttype = Contenttype;
Httpwebrequest. Referer = Referer;
Httpwebrequest. Accept = Accept;
Httpwebrequest. useragent = Useragent;
Httpwebrequest. Method = Method = Method. Post ?   " Post " : " Get " ;
Httpwebrequest. contentlength = Byterequest. length;

Stream stream = Httpwebrequest. getrequeststream ();
Stream. Write (byterequest, 0 , Byterequest. Length );
Stream. Close ();

Httpwebresponse;
Httpwebresponse = (Httpwebresponse) httpwebrequest. getresponse ();
Stream responsestream = Httpwebresponse. getresponsestream ();
Streamreader =   New Streamreader (responsestream, encoding. utf8 );
String Html = Streamreader. readtoend ();
Streamreader. Close ();
Responsestream. Close ();

Return HTML;
}

It is the packet capture result after the above Code is called in the program to access the login page. You can find that the login is successful:

In the next note, we will describe how to access the page that requires Logon (the relevant code will also be provided in the next note ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.