C ++ uses libcurl for httpclient

Source: Internet
Author: User

When C ++ is used as an HTTP client, the current common practice is to use libcurl. Its official website address is http://curl.haxx.se/, which hosts Curland libcurl. Curl is a command line tool used to complete FTP, ftps, HTTP, https, Gopher, telnet, dict, file, and LDAP command requests and receive feedback. Libcurl is provided to developers for developing requests and responses to various network protocols using C ++ cross-platform. The documents are very complete, but they are all in English.

This article provides the simplest demo to use libcurl to develop httpclient. It mainly includes synchronous http get, http post, https get, and HTTPS post.

Download libcurl package, if the use of Linux platform, it is recommended to download the source file compilation; if the use of Windows platform, it is recommended to download Win32-msvc, is: http://curl.haxx.se/download.html

# Ifndef _ http_curl_h __# DEFINE _ http_curl_h __# include <string> class chttpclient {public: chttpclient (void );~ Chttpclient (void); Public:/*** @ brief http post request * @ Param strurl input parameter, request URL address, such as: http://www.baidu.com * @ Param strpost input parameter, use the following format: para1 = val1 limit 2 = val2 &... * @ Param strresponse output parameter, returned content * @ return returns whether post is successful */INT post (const STD: string & strurl, const STD: string & strpost, STD :: string & strresponse);/*** @ brief http get request * @ Param strurl input parameter, request URL address, such as: http://www.baidu.com * @ Param strresponse output parameter, returned content * @ return whether post is successful */INT get (const STD: string & strurl, STD: string & strresponse);/*** @ brief https post request, no certificate version * @ Param strurl: Enter the request URL, for example, HTT. PS: // www.alipay.com * @ Param strpost: Enter the parameter in the following format: para1 = val1 limit 2 = val2 &... * @ Param strresponse output parameter, returned content * @ Param pcapath input parameter, which is the path of the CA certificate. if the input is null, the validity of the server certificate is not verified. * @ return indicates whether post is successful */INT posts (const STD: string & strurl, const STD: string & strpost, STD: string & strresponse, const char * pcapath = NULL);/*** @ brief https get request, no certificate version * @ Param strurl input parameters, request URL address, such: https://www.alipay.com * @ Param strresponse output parameter, returned content * @ Param pcapath input parameter, the path of the CA certificate. if the input is null, the validity of the server certificate is not verified. * @ return returns whether post is successful */INT gets (const STD: string & strurl, STD: string & strresponse, const char * pcapath = NULL); public: void setdebug (bool bdebug); Private: bool m_bdebug;}; # endif

# Include "httpclient. H" # include "curl/curl. H" # include <string> chttpclient: chttpclient (void): m_bdebug (false) {} chttpclient ::~ Chttpclient (void) {} static int ondebug (curl *, curl_infotype itype, char * pdata, size_t size, void *) {If (itype = curlinfo_text) {// printf ("[text] % s \ n", pdata);} else if (itype = curlinfo_header_in) {printf ("[header_in] % s \ n ", pdata);} else if (itype = curlinfo_header_out) {printf ("[header_out] % s \ n", pdata);} else if (itype = curlinfo_data_in) {printf ("[data_in] % s \ n", pdata);} else if (itype = curlinfo_data_out) {printf ("[data_out] % s \ n ", pdata);} return 0;} static size_t onwritedata (void * buffer, size_t size, size_t nmemb, void * lpvoid) {STD: string * STR = dynamic_cast <STD :: string *> (STD: string *) lpvoid); If (null = STR | null = buffer) {return-1 ;} char * pdata = (char *) buffer; str-> append (pdata, size * nmemb); Return nmemb;} int chttpclient: Post (const STD: string & strurl, const STD: string & strpost, STD: string & strresponse) {curlcode res; curl * curl = curl_easy_init (); If (null = curl) {return curle_failed_init ;} if (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (curl, scheme, ondebug);} curl_easy_setopt (curl, curlopt_url, strurl. c_str (); curl_easy_setopt (curl, curlopt_post, 1); curl_easy_setopt (curl, curlopt_postfields, strpost. c_str (); curl_easy_setopt (curl, curlopt_readfunction, null); curl_easy_setopt (curl, response, onwritedata); curl_easy_setopt (curl, response, (void *) & strresponse ); curl_easy_setopt (curl, scheme, 1); curl_easy_setopt (curl, scheme, 3); curl_easy_setopt (curl, scheme, 3); Res = curl_easy_perform (curl); curl_easy_cleanup (curl ); return res;} int chttpclient: Get (const STD: string & strurl, STD: string & strresponse) {curlcode res; curl * curl = curl_easy_init (); if (null = curl) {return curle_failed_init;} If (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (curl, callback, ondebug );}
Curl_easy_setopt (curl, curlopt_url, strurl. c_str (); curl_easy_setopt (curl, curlopt_readfunction, null); curl_easy_setopt (curl, response, onwritedata); curl_easy_setopt (curl, response, (void *) & strresponse ); /*** when multiple threads use timeout processing, sleep or wait operations are performed in the main thread. * If this option is not set, libcurl will send a signal to interrupt the wait and cause the program to exit. */Curl_easy_setopt (curl, scheme, 1); curl_easy_setopt (curl, scheme, 3); curl_easy_setopt (curl, curlopt_timeout, 3); Res = curl_easy_perform (curl ); curl_easy_cleanup (curl); Return res;} int chttpclient: Posts (const STD: string & strurl, const STD: string & strpost, STD: string & strresponse, const char * pcapath) {curlcode res; curl * curl = curl_easy_init (); If (null = curl) {return signature;} If (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (curl, curlopt_debugfunction, ondebug);} curl_easy_setopt (curl, curlopt_url, strurl. c_str (); curl_easy_setopt (curl, curlopt_post, 1); curl_easy_setopt (curl, curlopt_postfields, strpost. c_str (); curl_easy_setopt (curl, curlopt_readfunction, null); curl_easy_setopt (curl, response, onwritedata); curl_easy_setopt (curl, response, (void *) & strresponse ); curl_easy_setopt (curl, curlopt_nosignal, 1); If (null = pcapath) {curl_easy_setopt (curl, success, false); curl_easy_setopt (curl, success, false );} else {// PEM by default, so no need to set it. Der // curl_easy_setopt (curl, curlopt_sslcerttype, "PEM"); curl_easy_setopt (curl, curlopt_ssl_verifypeer, true) is also supported ); curl_easy_setopt (curl, scheme, pcapath);} Scheme (curl, scheme, 3); curl_easy_setopt (curl, scheme, 3); Res = curl_easy_perform (curl); curl_easy_cleanup (curl ); return res;} int chttpclient: gets (const STD: string & strurl, STD: string & strresponse, const char * pcapath) {curlcode res; curl * curl = curl_easy_init (); If (null = curl) {return dependencies;} If (m_bdebug) {curl_easy_setopt (curl, curlopt_verbose, 1); curl_easy_setopt (curl, expires, expires, ondebug);} curl_easy_setopt (curl, curlopt_url, strurl. c_str (); curl_easy_setopt (curl, curlopt_readfunction, null); curl_easy_setopt (curl, response, onwritedata); curl_easy_setopt (curl, response, (void *) & strresponse ); curl_easy_setopt (curl, curlopt_nosignal, 1); If (null = pcapath) {curl_easy_setopt (curl, success, false); curl_easy_setopt (curl, success, false );} else {curl_easy_setopt (curl, scheme, true); curl_easy_setopt (curl, scheme, pcapath);} curl_easy_setopt (curl, scheme, 3); curl_easy_setopt (curl, scheme, 3 ); res = curl_easy_perform (curl); curl_easy_cleanup (curl); Return res ;} //////////////////////////////////////// //////////////////////////////////////// ///////////// void chttpclient:: setdebug (bool bdebug) {m_bdebug = bdebug ;}

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.