Affirming: Pure technical exchange, do not use it! ! The scope of the technical application discussed in the article is still a wide range, in addition to voting this comparative application, you can also complete, such as automatic fill in the form, email automatic application, website registration. There are two methods provided here: Method 1: Realize the COM interface provided by the IE control. This approach is to edit web forms (sometimes modify web code) through the IHTMLDocument2 interface, and then trigger an event submitting web page. This approach is used to download a lot of data-independent data, such as a picture, etc., such as a picture, etc. (of course you can turn off the options in IE), the efficiency is very low, and each vote I have to open an IE control, if your machine is different, wait to press RESET ~~. In addition, this method is more encoded and is very annoying. Method 2: The work submitted by the Wininet API is implemented. This approach is much better than the previous method, not only high efficiency, less resource, but also simpler than the previous one. The following will introduce two methods: Method 1: Implement 1 through the COM interface provided by IE control 1, my program is based on dialog, not HTMLVIEW, so put a IE control on the dialog (Insert ActiveX Control, there is a Microsoft Web browser that gives this IE control, such as M_CtrLweb. Also, please add the
// Function: Decision web pages do not have a strname specified element // parameter: POBJALLELEMENT: Collection of all elements in the web page // strname: Id or Name Bool Hasitem (IhtmlelementCollection * Pobjallelelement, CString Strname) {CComptrin Idispatch> PDISP; POBJALLELEMENT-> Item (Colevariant (Strname), Colevariant ((long) 0), & PDISP); if (PDISP == NULL) Return False; Else Return True;} // Features: In the text box of the web page Enter strings // parameter: POBJALLELEMENT: Collection of all elements in the web page // strname: Id or name // strtext to edit text box: IHTMLELEMENTCOLLECTION * Pobjallelelement, CString Strname , CString strText) {CComPtr pDisp; pobjAllElement-> item (COleVariant (strName), COleVariant ((long) 0), & pDisp); CComQIPtr pElement; if (pDisp == NULL) {AfxMessageBox (strName "not found!") } Else {Pelement-> put_value (strtext.allocsystring ());}} // function: Submit a web // parameter: POBJALLELEMENT: Collection of all elements in the web page // strName: You can submit the FORM button id or name (may be directly Form to submit submitted) void SubmitPage (IHTMLElementCollection * pobjAllElement, CString strName) {CComPtrpDisp; pobjAllElement-> item (COleVariant (strName), COleVariant ((long) 0), & pDisp); CComQIPtrpElement; if ( PDISP ==
NULL) {AFXMessageBox (Strname "No!");} Else {pelement = pdisp; pelement-> click ();}} // function: Select a checkbox in the web page (in fact, click) // Parameters: Pobjallelement : // strName page collection of all the elements: the selected CheckBox to the id or name void CheckItem (IHTMLElementCollection * pobjAllElement, CString strName) {CComPtr pDisp; pobjAllElement-> item (COleVariant (strName), COleVariant ((long) 0) , & pdisp); ccomqiptr
Also assume that the voting page is http://www.onlytest.com/vote.htm, the data is submitted to http://www.onlytest.com /vote2.aspvoid cvotedlg :: OndownloadcompleteExplorer () {// Todo: Add Your Control NOTIFICATION handler code here IHTMLElementCollection * objAllElement = NULL; IHTMLDocument2 * objDocument = NULL; CString strUrl, strTemp; strUrl = m_ctrlWeb.GetLocationURL (); // get URL if the current page (strUrl.IsEmpty ()) return; objDocument = (IHTMLDocument2 * M_ctrlweb.getdocument (); // Get the IHTMLDocument2 interface pointer ObjDocument-> get_all (& objallelement); // Get a collection of all elements of the page // Since all pages are running, this function is executed, so you must decide the message according to the URL Source web IF (strurl == "http://www.onlytest.com/vote.htm") {ccomptr
http://www.onlytest.com/vote2.asp ") {enddialog (idok); // If the voting handle page has been downloaded, the program is ended, the reason is seen.}} The ticket is already thrown out, but see The process sequence you may be strange, why do you have an error to end the program with an endDialog in the middle, not to continue to vote? Things are like this, some websites can only vote, and an IE control is created, After connecting to the server (with sessions), the session key is set (a family), so if you continue to vote with this IE control, the server will tell you that you have already voted (of course, if the stupid written by the voting program, no management This, then it is much simpler). This problem I want to deal with the running process of Wininet API, but it seems to be very trouble, so I use a very stupid but simple way: voting the program as a program, then another The program calls this voting program. After the ticket is completed, the main program runs the voter again, so repeatedly. As for something such as the number of voters, the shared memory segment is the easiest (a family saying), not Here, this method is discussed here. Method 2: This method is very small through the Wininet API to implement a form, and it does not need to download too much useless data (such as pictures, etc.), and because you don't need to download too much useless data (such as pictures, etc.). Therefore, the efficiency is much higher, and the code is implemented is a function. It is very suitable for use in the thread. The key to use this method is to know what data should be submitted to the server if you go to the web file, and then analyze what should be submitted to the server? Data, the web page is very simple, if the web is very complicated, then it is a matter of labor. Now it is not a test. We will not do it. Now there is a simpler method, which is the network under Win2000. Monitor, manual investment, see what data is submitted to the server. In this way, we can put the data in the data belonging to the HTTP protocol. Data directly from the monitor is not used, because monitoring The part of the display text is replaced by a variety of characters such as the cycle. These parts must be changed back to the original carriage return, the wrap (the http header can do not need to manage, as long as you can distinguish the border. Also note that in submission information may have information information, if you have modified the contents of the submitted data, and the data length changes, the value of the Content-Length item must be changed. For example, Content-Length is originally The value is 100, there is a data "1" in the data, and now it is changed to "12", then content-leng TH must be changed to 101, otherwise the server will return an error.
The following is the voting function: uint vote (lpvoid) {cinternetSession session; theapp.m_nthreads ; // Used to record the Try {ChttpConnection * PConnection = session.gethttpConnection ("www.onlytest.net"); / / Website Server Chttpfile * Pfile = PConnection-> OpenRequest (chttpConnection :: http_verb_post, "vote2.asp"); // Directly submit data directly to the voting processing page Add a HTTP header to the submission data, which can be Get pfile-> addrequestheaders ("accept: image / gif, image / x-xbitmap, image / jpeg, image / pjpeg, application / vnd.ms-powerpoint, application / vnd.ms-excel, application / msword, * / * "); Pfile-> addRequestHeaders (" refere: http://www.onlytest.net/vote.htm "); Pfile-> AddRequestHeaders (" Accept-language: zh-cn "); Pfile-> AddRequestHeaders (" Content -Type: Multipart / Form-Data; Boundary = -------------------------- 7D11DC24268052C "); Pfile-> AddRequestHeaders (" accept- Encoding: gzip, deflate "); Pfile-> AddRequestHeaders (" User-agent: mozilla / 4.0 (compatible; msie 5.01; windows nt 5.0) "); Pfile-> AddRequestHeaders (" Content-le NGTH: 1351 "); Pfile-> AddRequestHeaders (" Connection: Keep-alive "); Pfile-> AddRequestHeaders (" cache-control: no-cache "); // The HTTP head should be true data, below TheApp.m_strformData is the data to be submitted, the server processes the information returned in Pfile-> SendRequest (Null, 0, THEAPP.M_STRFORMDATA.GETBUFFER (0), THEAPP.M_STRFORMDATA.GETLENGTH ()); // Submit all data // In fact, the voting here is already over, but if you want to see the results, you can analyze the return page, get some data char szbuffer [11001]; // Used to store the return processing page, how big The actual situation.
Of course, Int nlen = pfile-> read (Szbuffer, 11000); // read the return result, the HTML code, the HTML code, is actually the HTML code of the voting result page, is, cstring straTemp = szbuffer // cstring although it is abused, but it is convenient, 嘿嘿 ~ pfile-> close (); // Data read out to turn the closing thing to PConnection-> close (); delete pfile; delete PConnection Session.close (); // The following code is to analyze the data you are interested in, and there is no relationship with the vote, it doesn't explain int npos = strtemp.find ("Option A"); INT NTEMPPOS = NPOS; IF (NpOS == - 1) {THEAPP.M_NTHREADS -; return 0;} npos = startp.find ("Table Width = 100>