The C + + open Source Library Curl

Source: Internet
Author: User
Tags http post

About the Curl Library

Curl is a file transfer tool that works with URL syntax in the command line mode. It supports many protocols: FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. Curl not only provides an executable tool library, but also provides a Libcurl library for program development, which is written in C language and supports cross-platform, Libcurl points here. There is a project folder in the downloaded installation package, which is full of Visual Studio engineering libraries that have been provided to users, including versions of Visual Studio, provide code samples and library compilation, and also provide a variety of compilation methods, including static libraries, dynamic libraries, debug versions, The release version, the so-called industry conscience. The list of available versions is as follows:



Compiling the installation

Compiling the Curl Library is simple, find your corresponding vs directory, then open the project and select the desired version to compile. I am using the static library version of VS2010, so select the VC10 directory->lib Debug version, then compile, After compiling the libcurld.lib and corresponding debug information file libcurld.pdb, so that we develop debugging only need to put the library file and the Curl Library header file Folder curl into our project can use the Curl Library to provide us with the functionality. However, it is important to note that because of the particularity of curl, you need to predefine some macros and add a specific dependent library as follows:

You need to define a macro, otherwise you will be prompted not to find the definition of some function # building_libcurl//#define HTTP_ONLY//Dependent Library #pragma comment (lib, "Ws2_32.lib") #pragma Comment (lib, "Wldap32.lib") #pragma comment (lib, "Libcurld.lib")

List of main functions

  • Global initialization
    Curlcode Curl_global_init (long flags);
    Description: This function can only be used once (in fact, after the call to the Curl_global_cleanup function can still be used), if the function is not called when the Curl_easy_init function, it is said by the Libcurl library automatically completed.
    Parameters: Flags
    Curl_global_all//Initialize all possible calls.
    CURL_GLOBAL_SSL//initialization supports Secure Sockets Layer.
    CURL_GLOBAL_WIN32//Initialize Win32 socket font.
    Curl_global_nothing//No additional initialization

  • Global cleanup
    void Curl_global_cleanup (void);
    Description: Used to clean up the work done by Curl_global_init at the end of Libcurl. A function similar to close.

  • Get the Curl version library
    Char *curl_version ();
    Description: Prints the version of the current Libcurl library.

  • Initializing a Curl session
    CURL *curl_easy_init ();
    Description: Curl_easy_init is used to initialize a pointer to curl (Some like a pointer that returns a file type), and is cleaned with the Curl_easy_cleanup function at the end of the call. General Curl_easy_init means the beginning of a session, and its return value is generally used in the easy series of functions.
  • Cleaning up cur sessions
    void Curl_easy_cleanup (Curl *handle);
    Description: This call is used to end a session with Curl_easy_init.

    Parameter: A pointer of type curl.
  • Set Curl Session Parameters
    Curlcode curl_easy_setopt (Curl *handle, curloption option, parameter);
    Description: This function is most important, and almost all of the curl programs use it frequently to set parameters. It tells the Curl library how the program will behave, such as the HTML code to view a Web page.
    Pointer to parameter Handle:curl type
    Parameter option: Options for various curloption types. (both are defined in the Curl.h library and can be viewed by man)
    Parameter parameter: This parameter can be either a pointer to a function, a pointer to an object, or a long variable, depending on the second argument.

  • Perform a Curl session
    Curlcode curl_easy_perform (Curl *handle);
    Description: This function is called after initializing the pointer of the curl type and curl_easy_setopt, as the literal meaning of the perform is like a stage, allowing us to set the option to work and perform the actions set by curl.
    Parameters: pointer of type curl

code example (reference C + + uses Libcurl to do httpclient)

A class that encapsulates the post and get methods with curl

#ifndef __my_curl_h__#define __my_curl_h__#define building_libcurl//#define Http_only#include <string>class Chttpclient{public:chttpclient (void), ~chttpclient (void);p ublic:/*** @brief HTTP POST request * @param strurl input parameters, requested URL address, such as: http://www.baidu.com* @param strpost input parameters, using the following format para1=val1¶2=val2&...* @param strresponse output parameters, the contents returned * @return  Returns whether Post succeeded */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, return content * @return returns whether Post succeeded */int Get (const std::string & strURL, std::string & strresponse),/*** @brief HTTPS POST request, no certificate version * @param strurl input parameter Number, the requested URL address, such as: https://www.alipay.com* @param strpost input parameters, using the following format para1=val1¶2=val2&...* @param strresponse output parameters, What is returned * @param the Pcapath input parameter, the path to the CA certificate. If the input is null, the validity of the server-side certificate is not verified. * @return Returns whether Post succeeded */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 parameter, requested URL address, such as: https://www.alipay.com* @param the strresponse output parameter, return the contents of the * @param pcapath input parameter, the path to the CA certificate. If the input is null, the validity of the server-side certificate is not verified. * @return Returns whether post is successful */int Gets ( Const std::string & strURL, std::string & strresponse, const char * pcapath = NULL);p ublic:void setdebug (bool Bdeb UG);p rivate:bool m_bdebug;}; #endif

#include "my_curl.h" #include "curl/curl.h" #include <string> #pragma comment (lib, "Ws2_32.lib") #pragma comment ( LIB, "Wldap32.lib") #pragma comment (lib, "Libcurld.lib") chttpclient::chttpclient (void): M_bdebug (false) {} Chttpclient::~chttpclient (void) {}static int ondebug (CURL *, Curl_infotype itype, char * pData, size_t size, void *) {if (ity PE = = 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::s Tring*> ((std::string *) LPVOID); if (NULL = = STR | |    NULL = = buffer) {return-1;}    char* PData = (char*) buffer; Str->append (pData, size * nmemb); return nmemb;} int chttpclient::P ost (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, 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, Curlopt_writefunction, Onwritedata); curl_easy_setopt (Curl, Curlopt_writedata, (void *) &strresponse); Curl_easy_ Setopt (Curl, curlopt_nosignal, 1); curl_easy_setopt (Curl, curlopt_connecttimeout, 3); curl_easy_setopt (Curl, CURLOPT_ TIMEOUT, 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, curlopt_debugfunction, ondebug);} Curl_easy_setopt (Curl, Curlopt_url, strurl.c_str ()); curl_easy_setopt (Curl, curlopt_readfunction, NULL); curl_easy_ Setopt (Curl, curlopt_writefunction, onwritedata); curl_easy_setopt (Curl, Curlopt_writedata, (void *) &strresponse );/*** when multiple threads are using time-out processing, there is a sleep or wait operation 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, curlopt_nosignal, 1); curl_easy_setopt (Curl, curlopt_connecttimeout, 3); Curl_easy_setopt ( Curl, Curlopt_timeout, 3); res = curl_easy_perform (curl); Curl_easy_cleanup (curl); return res;} int chttpclient::P osts (const std::string & strurl, const std::string & Strpost, std::string & Strresponse, con St char * pcapath) {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, 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, curlopt_writefunction, onwritedata); curl _easy_setopt (Curl, Curlopt_writedata, (void *) &strresponse) curl_easy_setopt (Curl, curlopt_nosignal, 1); if (NULL = = Pcapath) {curl_easy_setopt (curl, Curlopt_ssl_verifypeer, false) curl_easy_setopt (curl, Curlopt_ssl_verifyhost, FALSE);} The else{//default is PEM, so no setup is required, additional support is der//curl_easy_setopt (Curl,curlopt_sslcerttype, "PEM"); curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, True); Curl_easy_setopt (Curl, Curlopt_cainfo, Pcapath);} Curl_easy_setopt (Curl, curlopt_connecttimeout, 3); curl_easy_setopt (Curl, curlopt_timeout, 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 curle_failed_init;}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_readfunction, NULL); curl_easy_ Setopt (Curl, curlopt_writefunction, onwritedata); curl_easy_setopt (Curl, Curlopt_writedata, (void *) &strresponse ); curl_easy_setopt (Curl, curlopt_nosignal, 1); if (NULL = = Pcapath) {curl_easy_setopt (curl, Curlopt_ssl_verifypeer, FALSE); Curl_easy_setopt (curl, Curlopt_ssl_verifyhost, false);} Else{curl_easy_setopt (Curl, Curlopt_ssl_verifypeer, true); Curl_easy_setopt (Curl, Curlopt_cainfo, Pcapath);} Curl_easy_setopt (Curl, curlopt_connecttimeout, 3); curl_easy_setopt (Curl, curlopt_timeout, 3); res = Curl_easy_perform (curl); Curl_easy_cleanup (curl); return res;} void Chttpclient :: Setdebug (bool bdebug) {m_bdebug = Bdebug;}
As can be seen from the above code, the use of the Curl Library is mainly divided into four steps: Init,setopt,perform and cleanup, where setopt is the most important to specify parameters

The C + + open Source Library Curl

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.