Automatic Webpage Logon (submitting post content) is widely used, such as identity verification, Program Upgrade and online voting. The following methods are implemented using C.
The core of Automatic Webpage logon and post submission is to analyze the webpage Source code (HTML). in C #, there are many HTML components that can be used to extract web pages. Generally, webbrowser, WebClient, and httpwebrequest are used. These three methods are used as follows:
1. webbrowser is a "mini" browser, which features no cookie or built-in JS issues during post.
Webbrowser is a new component provided by vs2005 (in fact, it encapsulates the IE interface). The post function is generally implemented by analyzing htmldocument in documentcompleted of webbrowser, Code As follows:
Htmlelement clickbtn = NULL; If (E. url. tostring (). tolower (). indexof ("xxx.htm")> 0) // login page { 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 "userctl ": Doc. All [I]. innertext = "user01 "; Break; Case "passct1 ": Doc. All [I]. innertext = "mypass "; Break; Case "B1 ": Clickbtn = Doc. All [I]; // submit button Break; } } } Clickbtn. invokemember ("click"); // click the button } |
2. WebClient encapsulates some HTTP classes and has simple operations. Compared with webbrowser, WebClient features self-configured proxy. Its disadvantage is Cookie control.
The WebClient runs in the background and provides the ability to perform asynchronous operations. This facilitates concurrent tasks and then waits for the results to be returned and then processes them one by one. The code for asynchronous multi-task invocation is as follows:
private void startloop (INT proxynum) {< br> WebClient [] wcarray = new WebClient [proxynum]; // initialization for (INT idarray = 0; idarray {< br> wcarray [idarray] = new WebClient (); wcarray [idarray]. openreadcompleted + = new openreadcompletedeventhandler (pic_openreadcompleted2); wcarray [idarray]. uploaddatacompleted + = new uploaddatacompletedeventhandler (pic_uploaddatacompleted2); try {< br> ...... wcarray [idarray]. proxy = new WebProxy (proxy [1], Port); wcarray [idarray]. openreadasync (New uri ("http://xxxx.com.cn/tp.aspId=129"); // open web; proxy = NULL; }< br> catch {< BR >} Private void pic_openreadcompleted2 (Object sender, openreadcompletedeventargs E) { If (E. Error = NULL) { String textdata = new streamreader (E. Result, encoding. Default). readtoend (); // obtain the returned information ..... String cookie = (WebClient) sender). responseheaders ["Set-cookie"]; (WebClient) sender). headers. Add ("Content-Type", "application/X-WWW-form-urlencoded "); (WebClient) sender). headers. Add ("Accept-language", "ZH-CN "); (WebClient) sender). headers. Add ("cookie", cookie ); String postdata = "......" Byte [] bytearray = encoding. utf8.getbytes (postdata); // convert it to a binary array (WebClient) sender). uploaddataasync (New uri ("http://xxxxxxy.com.cn/tp.aspId=129"), "Post", bytearray ); } } Private void pic_uploaddatacompleted2 (Object sender, uploaddatacompletedeventargs E) { If (E. Error = NULL) { String returnmessage = encoding. Default. getstring (E. Result ); ...... } }
|
3. httpwebrequest is relatively low-level and has many functions, cookie operations are also simple
private bool postwebrequest () {< br> cookiecontainer cc = new cookiecontainer (); string POS tdata = "user =" + struser + "& pass =" + strpsd; byte [] bytearray = encoding. utf8.getbytes (postdata); // convert httpwebrequest webrequest2 = (httpwebrequest) webrequest. create (New uri ("http://www.xxxx.com/chk.asp"); webrequest2.cookiecontainer = cc; webrequest2.method = "Post "; webrequest2.contenttype = "application/X-WWW-form-urlencoded"; webrequest2.contentlength = bytearray. length; stream newstream = webrequest2.getrequeststream (); // sendthedata. newstream. write (bytearray, 0, bytearray. length); // write parameter newstream. close (); httpwebresponse response2 = (httpwebresponse) webrequest2.getresponse (); streamreader Sr2 = new streamreader (response2.getresponsestream (), encoding. default); string text2 = sr2.readtoend (); ...... } |
httpwebrequest also provides asynchronous operations. If you are interested, check msdn.