Http Wrapper vs2008 Project

Source: Internet
Author: User
Tags call back http cookie http post

Http://files.cnblogs.com/files/kekec2/WinHttpClient_20100921.zip.gif

Http://www.codeproject.com/Articles/66625/A-Fully-Featured-Windows-HTTP-Wrapper-in-C

Features
    • Cookies supported
    • Proxy supported
    • GET, POST  Methods Supported
    • Request Headers Customization Supported
    • Disable Automatic redirection supported
    • HTTPS supported
    • Receive Progress supported
    • Some Other Features
Examplessimple Get Request

Get request is the most common request. Browsing a Web page causes one or several Get requests.

Hide Copy Code
The Set URL. Winhttpclient Client (L "/////// response content.wstring httpresponsecontent = client. Getresponsecontent ();     
Simple Post Request

Post request usually occurs while logging in or posting a thread.

Hide Copy Code
Winhttpclient Client (L"http://www.codeproject.com/");//Set Post data.string data ="Title=a_new_thread&content=this_is_a_new_thread. "; Client. Setadditionaldatatosend ((BYTE *) data.c_str (), data.size ());// Set request Headers. wchar_t szsize[50] = L ""; swprintf_s ( Szsize, L "%d", Data.size ()); wstring headers = L" content-length: "; headers + = Szsize;headers + l" \r\ncontent-type:application/x-www-form-urlencoded\r\n ", client. Setadditionalrequestheaders (headers); // Send HTTP Post Request.client.SendHttpRequest (L "post"); Wstring httpresponseheader = client. getResponseHeader (); wstring httpresponsecontent = client. Getresponsecontent ();               
Getting Request ' s Progress

You can specify a callback function to get the request ' s progress.

Hide Copy Code
progress-finished percentage. bool Progressproc (double progress) {    wprintf (L"void Progresstest (// Set URL and call back function. Winhttpclient Client (L"http://www.codeproject.com/", Progressproc), client. Sendhttprequest (); Wstring httpresponseheader = client. getResponseHeader (); Wstring httpresponsecontent = client. Getresponsecontent ();}        
Specifying the User Agent

User agent is a used by the clients to identify themselves to the Web server so, the string server can tell which Clien T software you use, Internet Explorer 8, Chrome or FireFox. Can specify the user agent to pretend Internet Explorer 8 to fool the Web server because sometimes the server on LY supports Internet Explorer 8.

Hide Copy Code
Winhttpclient Client (L"// Set The user agent to the same as Internet Explorer 8.client. Setuseragent (L"mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1;.) "); Client. Sendhttprequest (); wstring httpresponseheader = client. getResponseHeader (); wstring httpresponsecontent = client. Getresponsecontent ();    
Specifying the Proxy

Sometimes, we have to connect to the Web through proxies. WinHttpClient connects to the Web server directly and then uses the Internet Explorer setting to connect if it fails by default. You can also specify the proxy by calling function SetProxy .

Hide Copy Code
Winhttpclient Client (L"// Set the proxy to 192.168.0.1 with Port 8080.client. SetProxy (L"192.168.0.1:8080"); client. Sendhttprequest (); wstring httpresponseheader = client. getResponseHeader (); wstring httpresponsecontent = client. Getresponsecontent ();    
Handling Cookies

A cookie (also tracking cookie, browser cookie, and HTTP cookie) is a small piece of the text stored on a user's computer by a Web browser. A cookie consists of one or more name-value pairs containing bits of information.

The cookie is sent as an HTTP header by a Web server to a Web browser and then sent back unchanged by the browser each Tim E it accesses that server. A cookie can be used-authentication, session tracking (State maintenance), storing site preferences, shopping cart con Tents, the identifier for a server-based session, or anything else so can be accomplished through storing textual data ( Http://en.wikipedia.org/wiki/HTTP_cookie).

You can specify cookie to send by calling and SetAdditionalRequestCookies  get the response cookie by calling GetResponseCookies .

Hide Copy Code
Winhttpclient Client (L"// Set the cookie to send.client.SetAdditionalRequestCookies (L"// Get The response cookies.wstring httpresponsecookies = client. Getresponsecookies (); Wstring httpresponseheader = client. getResponseHeader (); wstring httpresponsecontent = client. Getresponsecontent ();     
HTTPSHide Copy Code
Winhttpclient Client (L"https://www.google.com/");  Accept any certificate while performing HTTPS request.client.RequireValidSslCertificates (false); Sendhttprequest (); wstring httpresponseheader = client. getResponseHeader (); wstring httpresponsecontent = client. Getresponsecontent ();    
Multiple requestsHide Copy Code
Winhttpclient Client (L"http://www.google.com/"); Sendhttprequest (); wstring httpresponseheader = client. getResponseHeader (); wstring httpresponsecontent = client. Getresponsecontent (); // Update the URL.CLIENT.UPDATEURL (L"http://www.microsoft.com/"); Sendhttprequest (); httpresponseheader = client. getResponseHeader (); httpresponsecontent = client. Getresponsecontent ();     
A Complete Example

CodeProject.com needs logging in to download the files. This example logs in, gets the cookie, requests the source code (win_http_wrapper/winhttpclient_src.zip) of my F Irst CodeProject article, A simple Windows HTTP Wrapper Using C + +, and then saves the file to hard disk. This example includes cookies handling, post requests, request headers customization, etc.

Hide Shrink Copy Code
//1. Get the initial cookie. Winhttpclient Getclient (L"Http://www.codeproject.com/script/Membership/LogOn.aspx "); Getclient.setadditionalrequestheaders (L"Accept:image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg, ... ");if (!getclient.sendhttprequest ()) {return;}//2. Post data to get the authentication cookie. Winhttpclient Postclient (L"Http://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2fscript%2fMembership%2fLogOn.aspx ");//Post data.wstring username = L"Yourcodeprojectusername "; wstring password = L"YourPassword ";p ostclient.setadditionalrequestcookies (Getclient.getresponsecookies ()); string data ="Formname=menubarform&email= ";d ata + = (char *) _bstr_t (USERNAME.C_STR ());d ata + ="&password= ";d ata + = (char *) _bstr_t (PASSWORD.C_STR ());d ata + ="&remembermecheck=1 ";p ostclient.setadditionaldatatosend ((BYTE *) data.c_str (), data.size ());//Post headers.wstring headers = L"... Content-length:%d\r\nproxy-connection:keep-alive\r\npragma:no-cache\r\n ";wchar_t Szheaders[max_path *[] = L""; swprintf_s (Szheaders, MAX_PATH *Headers.c_str (), Data.size ());p ostclient.setadditionalrequestheaders (szheaders);if (!postclient.sendhttprequest (L"post ", true)) {return;} // 3. Finally get the zip file. Winhttpclient downloadclient (L "win_http_wrapper/ Winhttpclient_src.zip ");d ownloadclient.setuseragent (L"  mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; ...)"); // sending this cookie makes the server believe you have already lo Gged in.downloadClient.SetAdditionalRequestCookies (Postclient.getresponsecookies ()); if (!downloadclient.sendhttprequest ()) {return;} Downloadclient.saveresponsetofile (L "C:\\WinHttpClient_ Src.zip ")               
Points of Interest
    • Sometimes, it's a good idea to get a piece of new code working first and improve it later.
    • Reading the hypertext Transfer Protocol (RFC 2616) would help a lot.
    • Use HTTP monitoring tools to help the development, such as HTTPAnalyzer  or HTTPWatch .
    • It's fast and easy-to-use class to _bstr_t convert between and wchar_t* char* .
History
    • 2010-9-21 2 Enhancements, thanks Scott Leckie
    • 2010-4-29 2 Bugs Fixed, thanks Wong Shao Voon
    • 2009-9 Fully Featured Version
    • 2008-7 Initial Version

Http Wrapper vs2008 Project

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.