Recently, I began to write programs in C #, mainly to access web pages and submit web forms. After a period of learning, you can finally compile an automatic logon tool. Of course, it is for personal applications. For example, I log on to my 163 mailbox, go to the internal network (changed to Renren), and log on to csdn. The interface is shown in the picture in the attachment. The program is very simple, as long as the login method of one of the websites has been written, the other is the same, you just need to change the ID of the fill element. Create a Windows application project, add the button you need in form, click the button, and log on to the corresponding website. The interface is as follows:
The central code is as follows:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
// Add reference
Using shdocvw; // located in the Windows/system32 directory
Using mshtml;
// The Code Template for each button control is as follows:
Private void button6_click (Object sender, eventargs E)
{// Log on to the 163 mailbox
Try
{
Shdocvw. internetexplorer Ie = new internetexplorer ();
Ie. Visible = true;
String url = "http://mail.163.com /";
Object nullarg = NULL;
Ie. navigate (URL, ref nullarg, ref nullarg );
System. Threading. thread. Sleep (3000 );
// Obtain the Document Object Model of IE
Mshtml. ihtmldocument2 dom = (mshtml. ihtmldocument2) IE. Document;
// Declare the user name
Mshtml. ihtmlinputtextelement txtusername = (mshtml. ihtmlinputtextelement) Dom. All. Item ("username", null );
Txtusername. value = "youremail ";
// Declare the password
Mshtml. ihtmlinputtextelement txtpwd = (mshtml. ihtmlinputtextelement) Dom. All. Item ("password", null );
Txtpwd. value = "yourpassword ";
// Declare Logon
Mshtml. htmlinputelement btnlogin = (mshtml. htmlinputelement) Dom. All. Item ("Logon email", 0); // login failure
System. Threading. thread. Sleep (1000 );
Btnlogin. Click ();
}
Catch (exception ex)
{
MessageBox. Show ("Logon Failed! ");
}
}
The csdn logon has a verification code, so the verification code can only be entered manually. If no verification code is entered, this program can easily find the type of website ID and type.
I have met a website. Its login key is not a button, but an image, so I cannot find its ID and do not know how to handle it. I am working hard. Also, I want to find a more common method that applies to various websites. This involves all aspects of automatic submission of web forms. Currently, I am a newbie and am learning, hope you can share your experience in this area!