C # automatically fill in the table and configure WebBrowserSubmit

Source: Internet
Author: User

It is very easy to write this stuff in C #. You only need to use a WebBrowser control to open the webpage for automatic table filling, find the specific page Member, assign values and trigger events, and then OK.

First, analyze the target page:

<Form name = "LoginForm" method = "get" action = "http: // 10. 245. ×. ×/login" onsubmit = "'Return">

<Table width = "253" border = "0" cellspacing = "0" cellpadding = "0">

<Tbody> <tr>

<Td colspan = "2"> </td>

</Tr>

<Tr>

<Td colspan = "2">

<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">

<Tbody> <tr>

<Td colspan = "3"> </td>

</Tr>

<Tr>

<Td width = "26"> </td>

<Td> User Account </td>

<Td width = "158"> <input type = "text" name = "username" size = "20" maxlength = "66">

</Td>

</Tr>

<Tr>

<Td width = "26"> </td>

<Td> User Password </td>

<Td width = "158"> <input name = "password" type = "password" id = "password" size = "20" maxlength = "23">

</Td>

</Tr>

<Input type = "hidden" name = "RecordPassword" value = "on">

<Input type = "hidden" name = "authmode" value = "CHAP">

<Input type = "hidden" name = "websuserip" value = "10.245.113.32">

<Input type = "hidden" name = "challenge" size = "50" value = "dhefbmmihpnfgmei">

<Input type = "hidden" name = "submittime" value = "0">

</Table>

</Td>

</Tr>

<Tr>

<Td height = "17"> <div align = "center">

<Input name = "clear" type = "button" value = "clear" onmouseout = "this. style. backgroundColor = '# d6efff' "onmouseover =" this. style. backgroundColor = '# 94D8FF' "onclick =" 'Return "> </div> </td>

<Td height = "27"> <div align = "center">

<Input name = "submit" type = "submit" height = "27" value = "login" onmouseout = "this. style. backgroundColor = '# d6efff' "onmouseover =" this. style. backgroundColor = '# 94D8FF' "> </div> </td>

</Tr>

</Table>

</Form>

You can know that you need to assign values to the input username and password, and then trigger the form submit event.

However, I encountered some problems when triggering submit, because submit is not a common registered event provided by C #, so it cannot be called directly through RaiseEvent.

I finally found the answer on a Chinese website. I can use AttachEventHandler ("submit", new EventHandler (fun) to add the event handler function fun to form, then, call the response function on the page using the InvokeMember method in fun. However, you may not be able to submit the url defined by the form action on the page (I did not try it ).

Therefore, another method provided on the website is modified. Using InvokeMember to call the click Event of the input button submit can also trigger the form submit, but the code is more concise.

The final C # code is as follows:

Private void Form1_Load (object sender, EventArgs e)

{

WebBrowser1.Navigate ("http: // 10. 245. *. */"); // open the target URL

}

Private void webbrowserappsdocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e)

{// WebBrowser completes page loading:

If (webBrowser1.Url. toString () = "http: // 10. 245. *. */") // It will be loaded again after the submit, and the name of the loaded page" logout "button is also submit, Khan, so you need to judge

{

HtmlDocument doc = webBrowser1.Document; // obtain the document Object

HtmlElement btn = null;

Foreach (HtmlElement em in doc. All) // round robin

{

String str = em. Name;

If (str = "username") | (str = "password") | (str = "submit") // reduce processing

{

Switch (str)

{

Case "username": em. SetAttribute ("value", "***"); break; // assign a user name

Case "password": em. SetAttribute ("value", "***"); break; // assign a password

Case "submit": btn = em; break; // obtain the submit button

Default: break;

}

}

}

Btn. InvokeMember ("click"); // triggers the submit event

// Doc. Forms ["LoginForm"]. InvokeMember ("submit ");

}

Else // close after successful login

{

This. Close ();

}

}

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.