①, WinInet and WinHTTP similarities and differences, why use WinHTTP explain?
For details see: "WinInet and WinHTTP Summary" document, has been packaged for everyone.
②, WinHTTP interface call mode:
Microsoft Official address: http://msdn.microsoft.com/en-us/... 84263 (v=vs.85). aspx
WinHTTP Microsoft provides two ways to call: The version of the C + + call API, and the version of the COM component. This is used in the C + + Interface interface components of the way to explain to you.
③, WinHTTP commonly used commands to explain:
For details see: http://msdn.microsoft.com/en-us/... 84263 (v=vs.85). aspx
/* ①, get the Web page source code steps: 1, the COM component initialization, 2, the definition WinHTTP object pointer, 3, creates and instantiates WinHTTP component, 4, calls open method opens the connection; 5, calls the Send method sends the request; 6, Gets the text returned by the ResponseText, 7, releases the WinHTTP Component object, 8, releases the WinHTTP object pointer, 9, uninstalls the COM component;*/
/* ②, advantages and disadvantages of using WINHTTP3 methods 1, type library generation wrapper class way: Advantages, easy to use, close to C + +, shortcomings->com interface default parameters must be filled, not flexible; 2, import+ interface: Advantages The application and release of COM interface control, very flexible, shortcomings, such as bad control, causing memory leaks, and COM component instantiation parameters are many; 3, import+ smart pointer wrapper class way: Advantages, smart pointer, easy to use, do not worry about memory leaks, COM component instantiation simple , the default default parameters for COM components can be used;
Here are some examples of the use of three methods:
Method 1:
//Method 1 type library generates wrapper classes: advantages, easy to use, close to C + +, disadvantage->com interface default parameters must be filled, not flexible;voidChttptestdlg::onbnclickedbutton1 () {CoInitialize (NULL); Cwinhttprequest* Pwinhttp =Newcwinhttprequest; BOOL BRet= Pwinhttp->createdispatch (L"winhttp.winhttprequest.5.1"); if(!bret)return;//clsid clsid = {0};//CLSIDFromProgID (L "winhttp.winhttprequest.5.1", &clsid);//Pwinhttp->createdispatch (CLSID);COleVariant Async=Variant_false; Pwinhttp->open (L"GET", L"http://www.baidu.com", async); Pwinhttp-Send (vtmissing); CString strsrc= pwinhttp->Get_responsetext (); MessageBox (STRSRC); Pwinhttp-ReleaseDispatch (); DeletePwinhttp; CoUninitialize ();}
Above,cwinhttprequest is a type library generation wrapper class
Method 2:
#import"C:\\windows\\syswow64\\winhttp.dll"No_namespace//Method 2voidChttptestdlg::onbnclickedbutton2 () {BSTR bstrbody; CString strbody; CoInitialize (NULL); Iwinhttprequest* Phttpreq =NULL; HRESULT HR=CoCreateInstance (__uuidof (WinHttpRequest), nullptr, Clsctx_all, __uuidof (iwinhttpreq uest), (PVOID*) &phttpreq); if(FAILED (HR)) {Goto_exit0; } HR= Phttpreq->open (L"GET", L"http://www.baidu.com"); if(FAILED (HR)) {Goto_exit1; } HR= phttpreq->Send (); if(FAILED (HR)) {Goto_exit1; } HR= Phttpreq->get_responsetext (&bstrbody);//<=> _bstr_t bstrrsp = phttpreq->responsetext; if(FAILED (HR)) {Goto_exit1; } strbody=Bstrbody; MessageBox (strbody); _exit1:phttpreq-Release (); _exit0:couninitialize ();}
Method 3:
#import"C:\\windows\\syswow64\\winhttp.dll"No_namespace//Method 3voidChttptestdlg::onbnclickedbutton3 () {BSTR bstrbody; CString strbody; CoInitialize (NULL); Iwinhttprequestptr Phttpreq=NULL; HRESULT HR=phttpreq.createinstance (__uuidof (winhttprequest)); if(FAILED (HR)) {Goto_exit0; } HR= Phttpreq->open (L"GET", L"http://www.baidu.com"); if(FAILED (HR)) {Goto_exit0; } HR= phttpreq->Send (); if(FAILED (HR)) {Goto_exit0; } phttpreq->get_responsetext (&bstrbody); Strbody=Bstrbody; MessageBox (strbody); _exit0:couninitialize ();}
Post Tutorial notes-WinHTTP get web Source