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 ).