123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- #include "CurlHttp.h"
- #include <regex>
- #include "fmtlog.h"
- static size_t WriteStringCallback(void* contents, size_t size, size_t nmemb, std::string* userStr)
- {
- size_t newLength = size * nmemb;
- size_t oldLength = userStr->size();
- try
- {
- userStr->resize(oldLength + newLength);
- }
- catch(std::bad_alloc &e)
- {
- FMTLOG_ERROR("{}", e.what());
- return 0;
- }
- std::copy_n((char*)contents, newLength, userStr->begin() + oldLength);
- return size * nmemb;
- }
- CurlHttp::CurlHttp()
- {
- curl_global_init(CURL_GLOBAL_DEFAULT);
- }
- CurlHttp::~CurlHttp()
- {
- curl_global_cleanup();
- }
- bool CurlHttp::Get(const std::string& url, std::string& response)
- {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if(curl == nullptr)
- {
- FMTLOG_ERROR("curl_easy_init() failed");
- return false;
- }
-
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
-
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
-
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
-
- bool result = true;
- res = curl_easy_perform(curl);
- if(res != CURLE_OK)
- {
- FMTLOG_ERROR("Get failed: {}, Url: {}", curl_easy_strerror(res), url);
- result = false;
- }
- curl_easy_cleanup(curl);
- return true;
- }
- bool CurlHttp::Get(const std::string& url, const std::vector<std::string>& vecHeader, std::string& response)
- {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if(curl == nullptr)
- {
- FMTLOG_ERROR("curl_easy_init() failed");
- return false;
- }
-
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
-
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-
- struct curl_slist *headers = NULL;
- if(!vecHeader.empty())
- {
- for(auto &header : vecHeader)
- {
- headers = curl_slist_append(headers, header.c_str());
- }
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- }
-
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
-
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
-
- bool result = true;
- res = curl_easy_perform(curl);
- if(res != CURLE_OK)
- {
- FMTLOG_ERROR("Get failed: {}, Url:{}", curl_easy_strerror(res), url);
- result = false;
- }
-
- curl_slist_free_all(headers);
- curl_easy_cleanup(curl);
- return result;
- }
- bool CurlHttp::Post(const std::string& url, const std::string& postData, std::string& response)
- {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if(curl == nullptr)
- {
- FMTLOG_ERROR("curl_easy_init() failed");
- return false;
- }
-
-
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
-
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
-
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
-
-
-
- res = curl_easy_perform(curl);
- bool result = true;
- if(res != CURLE_OK)
- {
- FMTLOG_ERROR("Post failed: {}, Url: {}", curl_easy_strerror(res), url);
- result = false;
- }
-
- curl_easy_cleanup(curl);
- return result;
- }
- bool CurlHttp::Post(const std::string& url,const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response)
- {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if(curl == nullptr)
- {
- FMTLOG_ERROR("curl_easy_init() failed");
- return false;
- }
-
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-
- curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
-
- struct curl_slist *headers = NULL;
- if(!vecHeader.empty())
- {
- for(auto &header : vecHeader)
- {
- headers = curl_slist_append(headers, header.c_str());
- }
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- }
-
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
-
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
-
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
-
-
-
- res = curl_easy_perform(curl);
- bool result = true;
- if(res != CURLE_OK)
- {
- FMTLOG_ERROR("Post failed: {}, Url: {}", curl_easy_strerror(res), url);
- result = false;
- }
- curl_slist_free_all(headers);
-
- curl_easy_cleanup(curl);
- return result;
- }
|