[Switch] C ++ using curl for http requests (GET, POST, Download) package

Original connection: https://www.cnblogs.com/oftenlin/p/9478067.html

CommonTools.h

  /*
 * CommonTools.h
 *
 *  Created on: 2018年8月2日
 *      Author: didi
 */
#include <iostream>
#include <curl/curl.h>
#include "zlib.h"
#include <vector>
#include <string>
#include <unistd.h>
#include <memory.h>
#include <json/json.h>
#include <sstream>
using namespace std;

class CommonTools{
    public:
        CommonTools();
        ~CommonTools();  public: static size_t receive_data(void *contents, size_t size, size_t nmemb, void *stream); // HTTP download files back off function size_t writedata2file static (void * ptr, size_t size, size_t nmemb, FILE * Stream) ; // int download_file file download interface static (const char * url, const char outfilename [ FILENAME_MAX ]) ; // http get requests CURLcode HttpGet static (const std :: String & strUrl, std :: String & strResponse, int nTimeout) ; // htpp POST request static CURLcode HttpPost (const std :: string & strUrl, std :: string szJson , STD :: String & strResponse, nTimeout int) ;}

 

CommonTools.cpp

  #include "CommonTools.h"
using namespace std;


size_t CommonTools::receive_data(void *contents, size_t size, size_t nmemb, void *stream){
    string *str = (string*)stream;
    (*str).append((char*)contents, size*nmemb); return size * nmemb; } size_t CommonTools::writedata2file(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written = fwrite(ptr, size, nmemb, stream); return written; } int CommonTools::download_file(const char* url, const char outfilename[FILENAME_MAX]){ CURL *curl; FILE *fp; CURLcode res; / * Call curl_global_init () * initialize libcurl / RES = curl_global_init (CURL_GLOBAL_ALL) ; IF (CURLE_OK =! RES) {the printf ( "libcurl the init failed.") ; Curl_global_cleanup () ; return -1 ; } / * curl_easy_init to call () function to get easy interface type pointer * / curl = curl_easy_init to () ; IF (curl) {FP = the fopen (outfilename, "WB") ; / * calls of curl_easy_setopt () set transmission options * / res = curl_easy_setopt (curl, CURLOPT_URL, url ) ; IF (RES =! CURLE_OK) {fclose (FP) ; curl_easy_cleanup (curl) ; return -1 ; } / * the of curl_easy_setopt () transfer option is set to achieve a user callback function to accomplish a specific task * / res = curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION to, CommonTools and :: writedata2file) ;! IF (RES = CURLE_OK) {fclose (FP); Curl_easy_cleanup (curl) ; return -1 ; } / * The of curl_easy_setopt () transfer option is set to achieve a user callback function to accomplish a specific task * / = RES of curl_easy_setopt (curl, CURLOPT_WRITEDATA, FP) ; IF (! RES = CURLE_OK) fclose {(FP) ; curl_easy_cleanup (curl) ; return -1 ; } curl_easy_perform RES = (curl) ; // call curl_easy_perform () function to complete the transfer tasks fclose (FP) ; / * the Check for errors * / IF (RES =! CURLE_OK) {fprintf (stderr, "curl_easy_perform () failed:% S \ n-", curl_easy_strerror (RES)) ; curl_easy_cleanup (curl) ; return -1 ; } / * * Always Cleanup / curl_easy_cleanup (curl) ; // call curl_easy_cleanup () frees the memory curl_global_cleanup} () ; return 0; } CURLcode CommonTools::HttpGet(const std::string & strUrl, std::string & strResponse,int nTimeout){ CURLcode res; CURL* pCURL = curl_easy_init();  if (pCURL == NULL) { return CURLE_FAILED_INIT;  } curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str()); //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(pCURL, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout); curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, CommonTools::receive_data); curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse); res = curl_easy_perform(pCURL); curl_easy_cleanup(pCURL); return res; } CURLcode CommonTools::HttpPost(const std::string & strUrl, std::string szJson,std::string & strResponse,int nTimeout){ CURLcode res; char szJsonData[1024]; memset(szJsonData, 0, sizeof(szJsonData)); strcpy(szJsonData, szJson.c_str()); CURL* pCURL = curl_easy_init(); struct curl_slist* headers = NULL; if (pCURL == NULL) { return CURLE_FAILED_INIT;  } CURLcode ret; ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str()); // std::cout << ret << std::endl;  ret = curl_easy_setopt(pCURL, CURLOPT_POST, 1L); headers = curl_slist_append(headers,"content-type:application/json");  ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);  ret = curl_easy_setopt(pCURL, CURLOPT_POSTFIELDS, szJsonData); //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);  ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, CommonTools::receive_data);  ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);  res = curl_easy_perform(pCURL); curl_easy_cleanup(pCURL); return res; }

 

Instructions

Main.cpp

  // GET request
string strURL = "http://www.baidu.com"; string strResponse; CURLcode nRes = CommonTools::HttpGet(strURL, strResponse,300); size_t nSrcLength = strResponse.length(); //下载文件 string strURL = "http://www.baidu.com/aaa.dat"; char local_file[50] = {0}; sprintf(local_file,"./pb_%d_%s.dat",1,"aaaa"); int iDownlaod_Success = CommonTools::download_file(url.c_str(),local_file); if(iDownlaod_Success<0){ char download_failure_info[100] ={0}; sprintf(download_failure_info,"download file :%s ,failure,url:",local_file,url); FATAL(download_failure_info); }

 

Guess you like

Origin www.cnblogs.com/lyggqm/p/11450555.html