Using WebBrowser to implement the automatic login function

Source: Internet
Author: User
Tags add time

Internal reform of the company, to make adjustments to the attendance, the need to achieve office automation, in OA for work attendance; As a programmer, how to realize automated attendance application?

The requirements are as follows:

    1. Can set attendance address, user information, commuting time;
    2. According to the set time of commuting, automatic attendance is realized.

The specific implementation steps are as follows:

    1. Form interface add time and attendance address, user information, work hours, WebBrowser control;
    2. Change the WebBrowser address to the attendance address
    3. After the WebBrowser load is completed, enter the user, password information, login into the system.
    4. Use the thread blocking form, estimate log in to enter the system time, and then enter the attendance menu for attendance

The code is implemented as follows:

     Public Partial classForm1:form {/// <summary>        ///The counter determines whether the Web page is loaded/// </summary>        inti =0; //Log in Timer        intj =0; //Work Timer        intm =0;  PublicForm1 () {InitializeComponent (); Txturl.enabled=false; Txtuser.enabled=false; Txtpwd.enabled=false; Tdtstarttime.enabled=false; Tdtendtime.enabled=false; WM=getwork (); Webbrowser.url=Wm.            URL; Webbrowser.documentcompleted+=NewWebbrowserdocumentcompletedeventhandler (webbw_documentcompleted); Webbrowser.navigated+=NewWebbrowsernavigatedeventhandler (webbrowser_navigated); }        /// <summary>        ///Use the counter to determine whether the interface is loaded/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        voidWebbrowser_navigated (Objectsender, WebBrowserNavigatedEventArgs e) {i++; }        voidWebbw_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) {i--; if(0==i) {if(J <1) {lognin (); J++; }                if(M <1&& J >0) {HTMLDocument Htmldoc=webbrowser.document; HtmlElement Txtmenucodequick= htmldoc.all["Menucodequick"]; if(Txtmenucodequick! =NULL) {txtmenucodequick.focus (); Txtmenucodequick.setattribute ("value","hr43b"); Sendkeys.sendwait ("{ENTER}"); M++; }                }            }        }        /// <summary>        ///Login/// </summary>        voidLognin () {HTMLDocument Htmldoc=webbrowser.document; HtmlElement Txtuser= htmldoc.all["Txtusercodel"];            Txtuser.focus (); if(Txtuser! =NULL) {Txtuser.setattribute ("value", Wm.            User);            } sendkeys.sendwait (Keys.Tab.ToString ()); HtmlElement Txtpassword= Htmldoc.getelementbyid ("txtpasswordl"); if(Txtpassword! =NULL) {Txtpassword.setattribute ("value", Wm.            Password); } thread.sleep ( +); HtmlElement Btndl= Htmldoc.getelementbyid ("Ctl00$ctl00$c1$cb$btnlogin"); Btndl.invokemember ("Click"); Thread.Sleep ( +); }        PrivateWorkmanger getwork () {Uri u=NewUri (Txturl.text); Uml URL=u; Uml User=Txtuser.text; Uml Password=Txtpwd.text; Uml Strattime=Tdtstarttime.time; Uml EndTime=Tdtendtime.time; returnWM; }        //opens a page based on the specified URL address        voidopenie (Uri url) {webbrowser.url=URL; }        PrivateWorkmanger WM =NewWorkmanger (); voidendwork () {HTMLDocument Htmldoc=webbrowser.document; HtmlElement Rtbtext= htmldoc.all["ctl00_ctl00_c1_h1_wsdbasictoolbar1"]; StringBuilder SB=NewStringBuilder (); foreach(HtmlElement iteminchhtmldoc.all) {sb. Appendline (item.            ID); } file.writealltext (@"D:\html.txt", sb.         ToString (), Encoding.UTF8); }    }    /// <summary>    ///attendance System class/// </summary>     Public classWorkmanger {/// <summary>        ///Address/// </summary>         PublicUri URL {Get;Set; } /// <summary>        ///User name/// </summary>         Public stringUser {Get;Set; } /// <summary>        ///Password/// </summary>         Public stringPassword {Get;Set; } /// <summary>        ///Working Hours/// </summary>         PublicDatetime? Strattime {Get;Set; } /// <summary>        ///Time off/// </summary>         PublicDatetime? EndTime {Get;Set; } }

Using WebBrowser to implement the automatic login function

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.