Use c # simulate website login

Source: Internet
Author: User

The first step we need to achieve when writing irrigation robots, capturing resource robots, and Web online game auxiliary tools is user login. So how to use C # to simulate a user's login pull to achieve user login, you must first understand how the General website judges whether the user is logged on.

HTTP is a connectionless protocol. That is to say, the content and status of this conversation have nothing to do with the previous one. To achieve persistent interaction with users, before a website and a browser establish a Session in the server memory, the Session identifies the user (browser) and each Session has a unique ID, when a session is established for the first time, the server sends the generated ID to the browser. In the following browser, each request sent to the server contains the SessionID, which indicates its identity.

The server uses memory to save information in the Session, so what does the browser use to save the SessionID pair allocated by the server? It is a Cookie. When a Session is created, the browser's request to the server does not contain the SessionID in the Cookie. The server considers the Session as a brand new Session and allocates a piece of memory to the Session, at the same time, the Session ID is sent to the browser using Set-Cookie in the Http Header.

Now that the Principles have been clarified, we can achieve a website login. The following uses the Management Information System of a certain University for testing (Note: The disadvantage here is that it is much less difficult to identify and redirect multiple servers without verification codes.

First, use a Dedicated Packet capture tool such as httpAnaly or httpwatch to obtain the data information and header information when the web page is submitted. The following code describes how to log on and obtain data on another page after logon.
1 private void Form1_Load (object sender, EventArgs e)
2 {
3
4 string username = "xxxx"; // User Name
5 string password = "xxxx"; // password
6 // create a new container for storing cookies
7 CookieContainer container = new CookieContainer ();
8 // splice post Data
9 string postData = ("username =" + username );
10 postData + = ("& passwd =" + password );
11 postData + = ("& login = % B5 % C7 % A1 % A1 % C2 % BC ");
12 ASCIIEncoding encoding = new ASCIIEncoding ();
13 byte [] data = encoding. GetBytes (postData );
14 HttpWebRequest request = (HttpWebRequest) WebRequest. Create ("http: // xxxx/login. asp ");
15 request. Method = "Post ";
16 request. ContentType = "application/x-www-form-urlencoded ";
17 request. ContentLength = data. Length;
18 request. KeepAlive = true;
19 request. CookieContainer = container; // The returned cookie is appended to this container.
20 // send data
21 Stream newStream = request. GetRequestStream ();
22 newStream. Write (data, 0, data. Length );
23 newStream. Close ();
24 // the following two sentences are indispensable
25 HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
26 response. Cookies = container. GetCookies (request. RequestUri );
27
28 HttpWebRequest requestScore = (HttpWebRequest) WebRequest. Create ("http: // xxxx/Score. asp ");
29 postData = "term = & TermList = % C7 % EB % D1 % A1 % D4 % F1 & ckind = & lwPageSize = 100 & lwBtnquery = % B2 % E9 % D1 % AF";
30 data = encoding. GetBytes (postData );
31 requestScore. Method = "Post ";
32 requestScore. ContentType = "application/x-www-form-urlencoded ";
33 requestScore. ContentLength = data. Length;
34 requestScore. KeepAlive = true;
35
36 // use the cookies for login to pass the subsequent verification
37 requestScore. CookieContainer = container;
38 Stream stream = requestScore. GetRequestStream ();
39 stream. Write (data, 0, data. Length );
40 stream. Close ();
41 HttpWebResponse responseSorce = (HttpWebResponse) requestScore. GetResponse ();
42 StreamReader reader = new StreamReader (responseSorce. GetResponseStream (), Encoding. Default );
43 string content = reader. ReadToEnd ();
44 textBox1.Text = content;
45
46}

Related Article

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.