Borrow curl 7.75.0 Version update , Recently, I downloaded it to play , Here's a brief note

1. Environment building

First of all libcurl Dynamic library , If you want to download the source code and compile it yourself https The agreement has to be downloaded OpenSSL and libssh Source code compiled together , I'm in trouble , So the official compiled dynamic library downloaded directly from the official website

linux Generally, it comes with or directly apt get It's very convenient

Put one here windows The download address of the environment :

The red box is curl The dependency Library of some functions , Here I suggest that you download them and throw them into the project directory

Download it and unzip it curl The directory structure is shown in the figure above , among bin It's a dynamic library , lib Is a static library , include It's the header file , It needs to be mentioned that lib Both static libraries in are .a At the end of the , Small and with dll Should be windows Version of , I changed the static library name to libcurl.lib

Finally, I'll look at the dynamic libraries that I might use , Static library , certificate , Header file integration , The contents are as follows :

Then link in your own program libcurl, contain curl The header file in the directory , take dll Put it in the same directory as the executable program and you can start to use it .

2. Call the interface to http Communications

Let's start with curl Basic process and important variables of request

(1) CURLcode curl_global_init() : This interface is used to initialize curl library , It should be in all curl Operation is called before

(2) CURL* curl_easy_init() : The interface returns a curl Handle , The type is CURL*, All operations related to a session are performed on this return handle

(3) void curl_easy_cleanup(CURL*) : This interface is used to release the given curl Handle , This interface should be called at the end of each session to release the corresponding curl Handle

(4) CURLcode curl_easy_setopt(CURL*, CURLoption, ...) : The interface can be set and specified by passing in different macros curl Handle properties , In order to control the various properties of the session

Here is an official link to check OPT The meaning of , It also includes official example :

(5) CURLcode curl_easy_perform(CURL*) : Execute a communication session through a given handle

(6) CURLcode : Almost all curl The return value of the interface is of this type , This code All defined curl State of operation , Here is an official link to check code The meaning of :

(7) const char* curl_easy_strerror(CURLcode) : take CURLcode To the corresponding meaning of the string, convenient for log output and other operations

That's what I use all the time curl Interface , curl Powerful , The protocols and content supported go far beyond http/https

The official evaluation is : libcurl is probably the most portable, most powerful and most often used network transfer library on this planet.

Here I encapsulate two functions , Namely http GET Request web pages and http GET Download the file , In the process cookie.

Code up :

Self encapsulated curl class

 1 class CHttpClient
2 {
3 public:
4 CHttpClient();
5 ~CHttpClient();
7 long http_enable_cookie(const char *path);
8 long http_post(const char *url);
9 long http_submit(const char *url, std::vector<std::string> &form);
10 long http_get(const char *url, std::string &body);
11 long http_download(const char *url, const char *fullpath);
12 long http_add_header(const char *header);
13 long http_add_multi_header(std::vector<std::string> &list);
15 private:
16 bool prepare_curl(const char *url);
17 bool exec_curl();
18 bool try_cleanup_curl();
20 private:
21 CURL *m_pCurl;
22 struct curl_slist *m_pHeader;
23 bool m_bSetCookie;
24 bool m_bSetHeader;
25 char m_szCookiePath[MAX_PATH];
26 };

Most important curl_global_init In the constructor , No more here , among prepare_curl, exec_curl, try_cleanup_curl For me curl http Basic encapsulation of communication flow

Here are three interfaces

 1 bool CHttpClient::prepare_curl(const char *url)
2 {
3 m_pCurl = curl_easy_init();
4 if (nullptr == m_pCurl) return false;
6 curl_easy_setopt(m_pCurl, CURLOPT_URL, url);
7 curl_easy_setopt(m_pCurl, CURLOPT_FOLLOWLOCATION, 1L);
8 curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
9 curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L);
11 if (m_bSetHeader)
12 {
13 curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, m_pHeader);
14 }
16 if (m_bSetCookie)
17 {
18 curl_easy_setopt(m_pCurl, CURLOPT_COOKIEJAR, m_szCookiePath); //set-cookie This path will be modified to correspond to cookie Cache file
19 curl_easy_setopt(m_pCurl, CURLOPT_COOKIEFILE, m_szCookiePath); // When the request is sent, it will be read from this file cookie
20 }
22 #ifdef DEBUG
23 curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, 1L);
24 curl_easy_setopt(m_pCurl, CURLOPT_DEBUGFUNCTION, cb_dbg);
25 #endif
27 return true;
28 }
30 bool CHttpClient::exec_curl()
31 {
32 CURLcode retCode = curl_easy_perform(m_pCurl);
33 try_cleanup_curl();
35 #ifdef DEBUG
36 print_dbg_msg();
37 #endif
39 if (CURLE_OK != retCode)
40 {
41 LOG_MSG(LOG_ERROR, "curl execute with code[%d] msg[%s]", retCode, curl_easy_strerror(retCode));
42 return false;
43 }
44 return true;
45 }
47 bool CHttpClient::try_cleanup_curl()
48 {
49 if (nullptr != m_pCurl)
50 {
51 curl_easy_cleanup(m_pCurl);
52 m_pCurl = nullptr;
53 }
55 if (m_bSetHeader)
56 {
57 curl_slist_free_all(m_pHeader);
58 m_pHeader = nullptr;
59 m_bSetHeader = false;
60 }
62 return true;
63 }

prepare_curl Mainly on curl Handle initialization , Set up http General parameters

exec_curl perform curl Communications , After the communication is completed, it is called try_cleanup_curl Memory free , And print debug Communications

in the light of prepare_curl in curl_easy_setopt Parameters of , Here's an explanation

(1)CURLOPT_URL : http Address of correspondence , Can resolve domain names

(2)CURLOPT_FOLLOWLOCATION : Follow page redirection

(3)CURLOPT_SSL_VERIFYPEER & CURLOPT_SSL_VERIFYHOST : Whether or not the two ends are SSL Security verification , Here I turn this off , Normal production environment will not do this , curl There are also certificates in the library , The old version may need to update the certificate to prevent some web pages from being inaccessible , I only do debugging here , It's more casual

(4)CURLOPT_HTTPHEADER : Set up http header, Incoming here curl_slist Structure , Use curl_slist_append You can directly const char* The type string is added to this structure , If not set , curl The default request header is GET, Accept,Host

(5)CURLOPT_COOKIEJAR : Specify this communication cookie Saved path , The save operation is in the corresponding curl Handle execution curl_easy_cleanup When the

(6)CURLOPT_COOKIEFILe : Specify this communication cookie Read path

(7)CURLOPT_VERBOSE : Set whether to echo the communication content , If the callback function is not specified after opening , Then use stderr

(8)CURLOPT_DEBUGFUNCTION : Set the callback function to call when echoing , The parameter list of the callback function should be (CURL *curl, curl_infotype type, char *data, size_t size, void *usr_ptr), among type Indicates the current data The type of , Types include CURLINFO_TEXT, CURLINFO_HEADER_IN, CURLINFO_HEADER_OUT, CURLINFO_DATA_IN, CURLINFO_DATA_OUT, CURLINFO_SSL_DATA_IN, CURLINFO_SSL_DATA_OUT, CURLINFO_END, The specific meaning refers to the official document, the actual debugging is easier to understand

Let's show CURLOPT_DEBUGFUNCTION The corresponding callback function and print_dbg_msg Print function

 1 static std::string g_sHeaderOut;
2 static std::string g_sHeaderIn;
3 static std::string g_sDataOut;
4 static std::string g_sDataIn;
6 int cb_dbg(CURL *curl, curl_infotype type, char *data, size_t size, void *usr_ptr)
7 {
8 switch (type)
9 {
11 g_sHeaderOut.append(data, size);
12 break;
14 g_sDataOut.append(data, size);
15 break;
17 g_sHeaderIn.append(data, size);
18 break;
20 g_sDataIn.append(data, size);
21 break;
22 default:
23 break;
24 }
25 return 0;
26 }
28 void print_dbg_msg()
29 {
30 if (!g_sHeaderOut.empty())
31 {
32 str_replace(g_sHeaderOut, "%", "%%");
33 LOG_MSG(LOG_DEBUG, "%s", g_sHeaderOut.c_str());
34 g_sHeaderOut.clear();
35 }
37 if (!g_sDataOut.empty())
38 {
39 str_replace(g_sDataOut, "%", "%%");
40 LOG_MSG(LOG_DEBUG, "%s", g_sDataOut.c_str());
41 g_sDataOut.clear();
42 }
44 if (!g_sHeaderIn.empty())
45 {
46 str_replace(g_sHeaderIn, "%", "%%");
47 LOG_MSG(LOG_DEBUG, "%s", g_sHeaderIn.c_str());
48 g_sHeaderIn.clear();
49 }
51 if (!g_sDataIn.empty())
52 {
53 str_replace(g_sDataIn, "%", "%%");
54 LOG_MSG(LOG_DEBUG, "%s", g_sDataIn.c_str());
55 g_sDataIn.clear();
56 }
57 }

Here because I write my own log to print and use vsprinf encounter % Will report a mistake , Here I've encapsulated another string Of replace Function % Replace with %%, It may not look good when printing , I haven't taken the time to optimize

Let's show GET Request and GET download request

 1 long CHttpClient::http_get(const char *url, std::string &res)
2 {
3 if (!prepare_curl(url)) return TSI_INTERNAL_ERR;
5 // CURLOPT_WRITEDATA The following parameters will be passed to the callback function usrdata
6 curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, cb_get);
7 curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &res);
9 if (!exec_curl())
10 {
11 LOG_MSG(LOG_ERROR, "http get fail");
13 }
14 return TSI_NO_ERR;
15 }
17 long CHttpClient::http_download(const char *url, const char *fullpath)
18 {
19 if (!prepare_curl(url)) return TSI_INTERNAL_ERR;
21 // Binary write mode creates Download File
22 FILE *download_file = fopen(fullpath, "wb");
23 if (nullptr == download_file)
24 {
25 try_cleanup_curl();
27 }
29 // Set the file handle to the download callback , curl Internally, the large file will be divided and callbacks will be called many times to write data
30 curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, cb_download);
31 curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, download_file);
33 //TODO: To determine if the download process is asynchronous , Prevent files from being downloaded , Just behind fclose 了
34 if (!exec_curl())
35 {
36 LOG_MSG(LOG_ERROR, "http download [%s] fail", fullpath);
37 fclose(download_file);
38 std::remove(fullpath);
40 }
42 LOG_MSG(LOG_INFO, "http download [%s] success", fullpath);
43 fclose(download_file);
44 return TSI_NO_ERR;
45 }

Two of them are involved CURLOPT, Here is an explanation

(1)CURLOPT_WRITEFUNCTION : This parameter specifies get Write method of the requested content , curl By default fwrite, The callback function parameter list must be (char *data, size_t size, size_t nmemb, void *usrdata)

(2)CURLOPT_WRITEDATA : This parameter passes the following data as a parameter to the specified writefunction in

Here are two callback functions cb_get and cb_download

 1 size_t cb_get(char *data, size_t size, size_t nmemb, void *usrdata)
2 {
3 size_t data_size = size * nmemb;
4 static_cast<std::string*>(usrdata)->append(data, data_size);
5 return data_size;
6 }
8 size_t cb_download(char *data, size_t size, size_t nmemb, void *usrdata)
9 {
10 size_t data_size = size * nmemb;
11 fwrite(data, size, nmemb, (FILE*)usrdata);
12 return data_size;
13 }

because download The function involves the analysis of specific websites , No debugging is shown here

Above is http get A simple example of a request , Common functions should also have form POST, Not for the time being , I'll make it up when I have time .

If there is any mistake or omission , Be sure to point out , Thank you very much. , At the same time, welcome to discuss related issues together , Reprint please indicate , thank !

C++ Use libcurl Conduct http More related articles in the Newsletter

  1. C++ use libcurl library http Communication network programming

    Use libcurl complete http Communications , Very convenient and thread safe , Reprint a good introductory article Reprinted from ...

  2. 【swupdate file 3、 ... and 】SWUpdate: Software upgrade of embedded system

    SWUpdate: Software upgrade of embedded system summary This project is believed to help update embedded systems from storage media or networks . however , It should be considered primarily as a framework , In this framework, you can easily add more protocols or installers to your application ( stay SWU ...

  3. ( turn ) utilize libcurl And two well-known IOT cloud communication routines in China , ubuntu and openwrt The debugging is successful ( Four )

    1. libcurl The reference documents for are as follows CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...

  4. C/C++ use libcurl library http Communication network programming

    C/C++ use libcurl library http Communication network programming indexes : One .LibCurl Basic programming framework Two . Some basic functions 3、 ... and .curl_easy_setopt Function part of the options Four .curl_easy_p ...

  5. C++ use libcurl library http Communication network programming 【 turn 】 C++ use libcurl library http Communication network programming indexes : One .Lib ...

  6. A bit of thinking about client technology ( For data storage SQLite, XMPP For communication Gloox, Web Interactive LibCurl, It's for data packaging Protocol Buffer, socket For communication boost asio)

    See today CSDN Last one <  Give up the declining completely MFC, Advice for new people !>, As a person who has been Windows Client development on the Internet C++ The programmer , There were similar worries a few years ago ( See   The feeling of being out of date ), Now there are some ...

  7. C++ use libcurl library http Communication network programming ( turn )

    Reprint : indexes : One .LibCurl Basic programming framework Two . Some basic functions 3、 ... and . ...

  8. C++ use libcurl library http Communication network programming [ turn ] indexes : One .LibCurl Basic programming framework Two . Some basic functions 3、 ... and .cur ...

  9. C++ use libcurl library http Network communication programming

      One .LibCurl Basic programming framework libcurl It's a cross platform network protocol library , Support http, https, ftp, gopher, telnet, dict, file, and ldap agreement .libcur ...

  10. libcurl library http Communications - The opening

    Let's start today with some frequently used libraries . First of all, let's write about libcurl library . libcurl The main function is to use different protocols to connect and communicate different server~ It's quite encapsulated sockPHP Support libcurl( I agree with you ...

Random recommendation

  1. ( turn ) Say goodbye to your doctorate by Wang Xun

    Say goodbye to your doctorate by Wang Yin After much deliberation , I decided to “ abandon ” My doctorate . This is the third time I've decided to leave my doctorate , It should be the last time . This should not be surprising news , Because although I'm a doctor 10 Years. , But my goal has always been ...

  2. mysql How to use nested in insert and select

    How to be in mysql Combine fields from multiple tables and insert them into a new table , Pass one sql Statements for . The specific situation is : There are three tables a.b.c, Now you need to start from the table b And table c Check the values of several fields in the table and insert them into the table a The corresponding field in . In this case , We can make ...

  3. The first set of questions in the north of Qing Dynasty (zhx)

    Death [ Problem description ] Now there's a place to play sif, There's a man waiting in line to call sif. Now let me tell you how long it took for each of the former individuals to play sif, When is the first time you can play sif.( The first person has to come in order ) [ Input format ] Two in the first line ...

  4. One , Fundamentals of entry —— 2. first project project

    1. On the right side of the welcome screen is a list of items , Show all recently opened projects , double-click ️ Open the previously created project . 2. Right click ️ It's about , choice "Add Files to DemoApp..." Add a picture . 3. ...

  5. AT&amp;T assembly

    AT&T Assembly and 8086 Assembly language is very similar , But it still can't be based on 8086 Read according to the rules of grammar AT&T It's compiled , So we still need to see AT&T The grammar rules of assembly , Because when reading kernel code , The hardware department ...

  6. C# Thread knowledge -- Use Task Perform asynchronous operations

    stay C#4.0 When you need to perform a complex asynchronous operation before , Only use CLR Thread pool technology to perform a task . When the thread pool performs asynchronous tasks , I don't know when the task will be finished , And the return value of the task cannot be obtained after the task is completed . But in C#4.0 I've got a good job in it ...

  7. Xamarin android How to decompile apk file

    Xamarin android How to decompile apk file Here's a recommendation XamarinAndroid The development of a small game , Stick hero , The game is very simple , It's really a little big . Wait a minute, let's translate this Xamarin Android ...

  8. Take-out food ERP Management system ( One )

    Take out in Jingmen era ERP Beijing Jingmen times Technology Co., Ltd. is a professional takeout ERP System management software . Ben ERP At present, Baidu has been connected . Are you hungry . Meituan and other delivery platforms , In the aspect of distribution, we have connected flash express delivery . Renren Express .UU Running errands and dada distribution . ...

  9. mysql Cross library replication : replicate_wild_do_table and replicate-wild-ignore-table

    Use replicate_do_db and replicate_ignore_db There is a hidden danger from time to time , There are errors when updating across Libraries . Such as setting replicate_do_db=testuse mysql;update test. ...

  10. springboot Of Web Development -Web Related configuration

    One :Spring Boot Provide automatic configuration By looking at WebMvcAutoConfiguration And WebMvcProperties Source code , You can find Spring Boot The following auto configuration is provided for us . 1, ...