Home>

Write a windows form application in c#language,Implement automatic login to a specific page.

The following uses automatic login:http://localhost/web/login.aspx as an example,Explain how to simulate manually entering a username and password and clicking login,Achieve automatic login.

Create a new c#application,Give the application a name,Such as autologin, add a textbox, button, and webbrowser controls to the form, and add the webbrowser1_documentcompleted event to the webbrowser control.

The button click event and the code of webbrowser1_documentcompleted are as follows:

private void btn_add_click (object sender, eventargs e)
{
 string surl=txb_url.text.trim ();
 if (surl.length>0)
 {
  webbrowser1.navigate (surl);
 }
}
private void webbrowser1_documentcompleted (object sender, webbrowserdocumentcompletedeventargs e)
{
 htmlelement clickbtn=null;
 if (e.url.tostring (). tolower (). indexof ("login.aspx")>0)
 {
  htmldocument doc=webbrowser1.document;
  for (int i=0;i<doc.all.count;i ++)
  {
   if (doc.all [i] .tagname.toupper (). equals ("input"))
   {
    switch (doc.all [i] .name)
    {
     case "txtusername":
      doc.all [i] .innertext="[email protected]";//username
      break;
     case "txtpassword":
      doc.all [i] .innertext="zzzzzz";//password
      break;
     case "btnsubmit":
      clickbtn=doc.all [i];
      break;
    }
   }
  }
  clickbtn.invokemember ("click");//click the "login" button
 }
}

After typing http://localhost/web/login.aspx into the textbox and clicking the button,You can automatically log in to the page.

c
  • Previous MFC method to append data at the end of the file
  • Next In-depth analysis of thread synchronization and inter-thread communication in Java