C # Web Page Automatic Logon for network programming (I). Use the webbrower control to simulate Logon

Source: Internet
Author: User

Recently I learned C # network programming. I want to automatically log on to the web page and submit get/post information, and then realize loop login to continuously send packets to the server, the server sends a message to the client to record the account and password that can be logged on, so as to bypass the verification code and dynamically capture the logon location. However, due to the small amount of information and slow progress, the following article is for your exchange and reading only.

You may have read an article about three methods for extracting webpage HTML in the C # post submission method: webbrowser, WebClient, and httpwebrequest. I have also read a lot of blogs, especially about webbrowser and httpwebrequest methods, but they have limited capabilities. Many things are still not successfully implemented and are still being studied.

Next, let's talk about the only thing I learned in this phase: Using webbrowser to achieve automatic login in a simple browser. Webbrowser is a simple browser that allows users to browse webpages in the control. In fact, it encapsulates the IE interface and implements the get/post function in documentcompeted of webbrowser.

In the "C # web programming the simplest browser implementation" (http://blog.csdn.net/eastmount/article/details/9490009), I completed a simple browser through the webbrowser control, here, we can continue to implement the web page automatic login function based on this program.

I. Interface Design

The preceding interface mainly adds a webbrowser control. For detailed parameters set, see web site: http://blog.csdn.net/eastmount/article/details/9490009

Key: click the "webbrowser1" control to add an event documentcorr to the Property dialog box. (All documents hosted in the Web browser control are loaded ).

The main implementation process of this program is:

1. click the button to add the simple browser function to the button event click;

2. Search for the username, password, and logon button HTML element in the documentcorr event, and assign values to set the logon status to Enable Automatic Logon.

Ii. Source Code

I have read a lot of Blog Code, but it is still very difficult to implement it by myself. So I have filled out all the code and detailed comments and implementation processes, hoping to help beginners, you can also consolidate your knowledge, right! At the same time, the most important thing is thinking.

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; // Add a new namespace using system. net; using system. io; namespace httpweb {public partial class form1: FORM {public form1 () {initializecomponent () ;}// double-click "Browse" to add the Click Event private void button#click (Object sender, eventargs e) {// get the input URL string url = textbox1.text; // string url = "http://mail.163.com/"; // create an HTTP link // httpwebrequest object instance: this class is used to obtain and operate HTTP request var. You can change it to httpwebrequest var request = (httpwebrequest) webrequest. create (URL); // create a webrequest object // httpwebresponse object instance: this class is used to obtain and operate HTTP Response var. You can change it to httpwebresponse var response = (httpwebresponse) request. getresponse (); // getresponse: Get a response // construct a data stream object instance stream = response. getresponsestream (); // getresponsestream: Get the response stream streamreader sr = new streamreader (Stream ); // read characters from the byte stream // read from the current position of the stream to the end and display string content = Sr in the webbrower control. readtoend (); webbrowser1.documenttext = content;} // after all the documents in the Web browser control are loaded, private void webbrowserappsdocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs e) occurs) {// define the HTML element to get the control value through name // htmlelement tbuserid = webbrowser1.document. all ["username"]; // htmlelement tbpasswd = webbrowser1.document. all ["password"]; // defines the HTML element to get the control value through ID (user name and password logon button) htmlelement tbuserid = webbrowser1.document. getelementbyid ("idinput"); htmlelement tbpasswd = webbrowser1.document. getelementbyid ("pwdinput"); htmlelement btnsubmit = webbrowser1.document. getelementbyid ("loginbtn "); // if one of the three elements is null, the value is assigned only after loading is returned. Otherwise, a null-value crash error occurs. If (tbuserid = NULL | tbpasswd = NULL | btnsubmit = null) {return;} // set the element value attribute value (user name and password value) tbuserid. setattribute ("value", "eastmount"); tbpasswd. setattribute ("value", "eastmount"); // method of executing an element, for example, click Submit btnsubmit. invokemember ("click ");}}}

In fact, the core code for using the webbrowser control to implement Automatic Webpage logon is:

// Obtain the webpage element (username and password logon button) htmlelement tbuserid = webbrowser1.document. getelementbyid ("idinput"); htmlelement tbpasswd = webbrowser1.document. getelementbyid ("pwdinput"); htmlelement btnsubmit = webbrowser1.document. getelementbyid ("loginbtn"); // assign tbuserid to the element using the corresponding method. setattribute ("value", "eastmount"); tbpasswd. setattribute ("value", "eastmount"); btnsubmit. invokemember ("click ");

Iii. Running results

The above source code is automatically login 163 mailbox implementation: http://mail.163.com run program, enter the Web site can achieve automatic login 163 mailbox.

On the logon page, you can find that the username ID is "idinput", the password ID is "pwdinput", and the logon button ID is "loginbtn ". Such as ID and name:

<Inputclass = "loginformtdipt" tabindex = "1" Title = "Enter the Account" id = "idinput" name = "username" type = "text" maxlength = "50" value = """..

<Inputclass = "loginformtdipt" tabindex = "2" Title = "enter the password" id = "pwdinput" name = "password" type = "password" AutoComplete = "off"/>

<Buttonid = "loginbtn" class = "btnbtn-Login
Formsubmit "tabindex =" 6 "type =" Submit "> login & nbsp; recording </button>

Note: The above values "eastmount" and "eastmount" are modified by me. You need to enter your own password and user name. At the same time, some garbled characters may occur during login. These are encoding problems. Some websites may be able to log on automatically through this method, while some websites may not, but I provide an idea!

During the operation, I first tried the Baidu website, but the Verification Code failed due to remote logon.

Local URL: http: // localhost: 1165/webtest/mylogin. to view the source code of aspx, you can set the user name to "username", the password ID to "password", the login button to "loginbutton", and the user name and password to 1, set the corresponding values to achieve logon. Display the local logon page for the browser:


Iv. Summary

In fact, due to my recent research on httpwebrequest and many cookies, I want to use this class to implement the same function. However, I have compiled several programs that are still not implemented and are still under study. I hope this article will help you, and I do not know whether my previous descriptions about webbrowser are correct. If there are any errors or deficiencies, I hope you can correct them. I hope you can talk more about them, learn from each other and make progress together!

At the same time, I would like to thank some bloggers. Below are some blogs about webbrowser. If you have time to read them, I think they are all excellent articles, at the same time, the core ideology I mentioned above is similar to the following. I would like to thank the following bloggers and articles: (by: eastmount
Four o'clock P.M)

1. C # automatically fill in the Automatic Logon form on the webpage-sonicrang (the simplest Code describes how to use webbrowser for automatic logon)

Http://blog.csdn.net/sonicrang/article/details/5878611

2. Use of the webbrowser control in C #-fang Pei Studio (describes in detail how to use the webbrowser Control)

Http://www.cnblogs.com/txw1958/archive/2012/09/24/CSharp-WebBrowser.html

3. C # implement the post submission method-Chen liguo (one of the most classic articles describes three methods in detail: webbrowser, WebClient, and httpwebrequest, but I do not know that they were originally written by the master character)

Http://www.cnblogs.com/cgli/archive/2011/04/09/2010497.html

5. C # automatically log on to the login code-Cherish the current situation (the complete code tells the login function, only the code)

Http://hi.baidu.com/zkbob22/item/da897035b65516362e0f8181

6. brute-force cracking of website passwords-Believe in yourself (a full brute-force cracking of Code focuses on thinking, universal password attempts, but I have not succeeded)

Http://blog.sina.com.cn/s/blog_4de05339010109g7.html

7. Discussion on the C # simulated login Forum (some of the Code is excellent)

Http://bbs.csdn.net/topics/330239749? Page = 1

8. programming code for automatic website login C # (similar to my idea)

Http://wenku.baidu.com/view/bc46ff9d6bec0975f465e208.html

9. C # winform webbrowser simulated logon logging-Kelvin peak (too advanced, to be further studied)

Http://blog.csdn.net/llftc/article/details/7036746

 

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.