A WinHTTP Service Program

Source: Internet
Author: User

The company has two sets of attendance systems, one is the door clock to go to work, and the other is the computer login attendance website to go to work. People like me who take the bus to work on a daily basis don't know when they will be late when a traffic jam occurs. So bored and helpless, I wrote the following program .... First, check the portal card system. Because you need to swipe your card to go to work, the card data is stored on the company's Linux server. So I switched to the website attendance system. This website is written in JSP, so I plan to use effetech HTTP sniffer to investigate it. I thought it was very difficult. In fact, the sniffing result was very simple, and the login request was packaged using URL rewrite technology. After intercepting this URL request, you can start simulating the request. In Java, socket can be used to send requests directly, but the Win32 platform SDK has a higher-level HTTP protocol packaging-WinHTTP. The following request sending class // ----------------------------------------------------------------------------------- // name: Class cmywinhttp // Desc: connect to the Web server, process the Interaction Based on http // your class cmywinhttp {hinternet m_hsession; hinternet m_hconnect; hinternet m_hrequest; public: cmywinhttp (); int Init (); void close (); bool PR Ocessonerequest (lpcwstr pwszobjectname);}; the constructor will not talk about it. The initialization member variable is null. The init function is relatively simple. The host and port are the Server IP address and port 80. /// --------------------------------------------------------------- // Desc: init the connection // unless the host/port/User-Agent changes... // ------------------------------------------------------------------- int cmywinhttp: Init () {// use winhttpopen to obtain a session handle. m_hsession = winhttpopen (user_agent, winhttp_access_type_no_proxy, winhttp_no_proxy_name, winhttp_no_proxy_bypass, 0 );// Specify an HTTP server. if (m_hsession) m_hconnect = winhttpconnect (m_hsession, host, port, 0); If (m_hconnect) return 0; return getlasterror ();} The close () function is not mentioned, close some handles. The packaging process for sending and processing GET requests is as follows: bool cmywinhttp: processonerequest (lpcwstr pwszobjectname) {DWORD dwsize = 0; DWORD dwdownloaded = 0; lpstr pszoutbuffer; bool bresults = false; // create an HTTP request handle. if (m_hconnect) m_hrequest = winhttpopenrequest (m_hconnect, l "get", pwszobjectname, null, winhttp_no_referer, winhttp_default_accept_types, 0); // send a request. if (m_hrequest) bresults = winhttpse Ndrequest (m_hrequest, additional,-1l, winhttp_no_request_data, 0, 0, 0); // end the request. if (bresults) bresults = winhttpreceiveresponse (m_hrequest, null); // keep checking for data until there is nothing left. if (bresults) {do {// check for available data. dwsize = 0; If (! Winhttpquerydataavailable (m_hrequest, & dwsize) printf ("error % u in winhttpquerydataavailable. /n ", getlasterror (); // allocate space for the buffer. pszoutbuffer = new char [dwsize + 1]; If (! Pszoutbuffer) {printf ("out of memory/N"); dwsize = 0;} else {// read the data. zeromemory (pszoutbuffer, dwsize + 1); If (! Winhttpreaddata (m_hrequest, (lpvoid) pszoutbuffer, dwsize, & dwdownloaded) printf ("error % u in winhttpreaddata. /n ", getlasterror (); else printf (" % s ", pszoutbuffer); // free the memory allocated to the buffer. delete [] pszoutbuffer;} while (dwsize> 0);} // report any errors. if (! Bresults) printf ("error % d has occurred. /n ", getlasterror (); Return bresults;} The main function only needs to be written in this way, where submit is the parsed GET request. Int _ tmain (INT argc, _ tchar * argv []) {cmywinhttp * mywinhttp = new cmywinhttp (); If (mywinhttp-> Init ()! = 0) Return-1; if (! Mywinhttp-> processonerequest (submit) Return-1; mywinhttp-> close (); Return 0;} by now, the power of jiuyang is slightly reduced, the following is how to make it run on its own every day. You can choose to put it in a scheduled task, but there is always trouble in setting it. So I plan to write a service and only run it once when the system starts up. Then I can set the BIOS of the machine to timed start. (The RP is good, but the BIOS of the machine has this function, in addition, you can also select autoboot only weekday, Dizzy ). The online Introduction to the compilation of system services is overwhelming. I can use the ntservice after modifying it. The class is designed as follows: // users // name: Class cservice // Desc: System Service Registration // your class cservice {DWORD dwattendanttime; tchar * lpservicename; public: cservice (); //~ Cservice (); static void winapi servicemain (DWORD argc, tchar * argv []); static void winapi servicecontrolhandler (DWORD controlcode); void installservice (); void uninstallservice (); void runservice (); service_status servicestatus; service_status_handle servicestatushandle; static cservice * m_this; handle stopserviceevent;}; two static member functions are used for callback of WIN32API. Ah, there is no good delegate for C. By the way, the high-performance C ++ delegate implementation on the Internet does not seem very practical. If high performance is not emphasized, you can use static member functions as the callback function. Note that there is a static member pointer m_this pointing to the class instance. In the callback function, you can use the member of the member class (this is totally incredible in Java, because there is no pointer in Java, therefore, Java can only use the internal class technology to complete GUI events ). Run the question. Let's look back at the servicemain () function.

// Configure // @ brief static function servicemain for callback // @ note entry point of the Service // configure void cservice: servicemain (DWORD argc, tchar * argv []) {cservice & thisservice = * m_this; struct TM * TM; time_t now; DWORD minnow; cmywinhttp * mywinhttp; th Isservice. dwattendanttime = generaterandomtime (); // initialise service status thisservice. servicestatus. dwservicetype = service_win32; thisservice. servicestatus. dwcurrentstate = service_stopped; thisservice. servicestatus. dwcontrolsaccepted = 0; thisservice. servicestatus. dwwin32exitcode = no_error; thisservice. servicestatus. dwservicespecificexitcode = no_error; thisservice. servicestatus. dwcheckpoi Nt = 0; thisservice. servicestatus. dwwaithint = 0; thisservice. servicestatushandle = registerservicectrlhandler (thisservice. lpservicename, servicecontrolhandler); If (thisservice. servicestatushandle) {// service is starting thisservice. servicestatus. dwcurrentstate = service_start_pending; setservicestatus (thisservice. servicestatushandle, & (thisservice. servicestatus); // do initialization th Isservice. stopserviceevent = createevent (0, false, false, 0); // running thisservice. servicestatus. dwcontrolsaccepted | = (service_accept_stop | service_accept_shutdown); thisservice. servicestatus. dwcurrentstate = service_running; setservicestatus (thisservice. servicestatushandle, & (thisservice. servicestatus); do {// BEEP (1000,100); // sleep (100); time (& now); TM = localtime (& now); minnow = TM-> Tm_min; // only exectue at 8: 00 ~ 9: 00 on weekday if (Tm-> tm_wday = 0) | (Tm-> tm_wday = 6) | (Tm-> tm_hour! = 8) {sleep (2000); setevent (thisservice. stopserviceevent); break;} If (thisservice. dwattendanttime + 38 <minnow) {// For example: now is 8: 45, Min is 26, then do following: mywinhttp = new cmywinhttp (); mywinhttp-> Init (); mywinhttp-> processonerequest (qianz_submit); mywinhttp-> close (); setevent (thisservice. stopserviceevent) ;}else {sleep (1000) ;}while (waitforsingleobject (thisservice. st Opserviceevent, 1000) = wait_timeout); // service was stopped thisservice. servicestatus. dwcurrentstate = service_stop_pending; setservicestatus (thisservice. servicestatushandle, & (thisservice. servicestatus); // do cleanup here closehandle (thisservice. stopserviceevent); thisservice. stopserviceevent = 0; // service is now stopped thisservice. servicestatus. dwcontrolsaccepted & = ~ (Service_accept_stop | service_accept_shutdown); thisservice. servicestatus. dwcurrentstate = service_stopped; setservicestatus (thisservice. servicestatushandle, & (thisservice. servicestatus);} Do you understand? Use cservice & thisservice = * m_this to access the member variable, and send the attendance and work requests to the server from AM to AM, and then disable the service automatically. In this way, you will not be late.
Related Article

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.