|
@@ -69,17 +69,92 @@ CurlHttp::~CurlHttp()
|
|
|
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;
|
|
|
}
|
|
|
- /* 设置用户名密码,可能有的话 */
|
|
|
+ /* 设置为Get请求 */
|
|
|
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
|
|
|
+ /* 设置url */
|
|
|
+ curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
+ /* 设置重定向,遇到3xx返回值时自动重定向 */
|
|
|
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
+ /* 设置https协议 */
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
+/* 获取信息,带有http头 */
|
|
|
+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;
|
|
|
+ }
|
|
|
+ /* 设置为Get请求 */
|
|
|
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
|
|
|
+ /* 设置url */
|
|
|
+ curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
+ /* 设置http头 */
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ /* 设置重定向,遇到3xx返回值时自动重定向 */
|
|
|
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
+ /* 设置https协议 */
|
|
|
+ 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;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* @brief 发送信息,不携带http头
|
|
|
*
|
|
@@ -103,22 +178,15 @@ bool CurlHttp::Post(const std::string& url, const std::string& postData, std::st
|
|
|
|
|
|
/* 设置动作功能和网址 */
|
|
|
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
|
|
|
- curl_easy_setopt(curl, CURLOPT_URL, "http://172.16.36.80:30000/vos/external/broadcastToken");
|
|
|
+ curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
/* 设置重定向,遇到3xx返回值时自动重定向 */
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
/* 设置https协议 */
|
|
|
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
|
|
|
|
|
|
- /* 设置http头 */
|
|
|
- struct curl_slist *headers = NULL;
|
|
|
- headers = curl_slist_append(headers, "User-Agent: Apifox/1.0.0 (https://apifox.com)");
|
|
|
- headers = curl_slist_append(headers, "Content-Type: application/json");
|
|
|
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
-
|
|
|
/* 设置包体 */
|
|
|
- const char *data = R"({"appSecret":"8zR9ug4WbKCtEgaQ08myRm5HqKIMwP83Ra24t921tO/mU0cTIFdm/t0C9Jxrd53x","appKey":"rili4l26"})";
|
|
|
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
|
|
|
- curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));
|
|
|
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
|
|
|
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
|
|
|
|
|
|
/* 设置回调函数 */
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteStringCallback);
|
|
@@ -137,7 +205,7 @@ bool CurlHttp::Post(const std::string& url, const std::string& postData, std::st
|
|
|
bool result = true;
|
|
|
if(res != CURLE_OK)
|
|
|
{
|
|
|
- FMTLOG_ERROR("curl_easy_perform() failed: {}", curl_easy_strerror(res));
|
|
|
+ FMTLOG_ERROR("Post failed: {}, Url: {}", curl_easy_strerror(res), url);
|
|
|
result = false;
|
|
|
}
|
|
|
|
|
@@ -158,7 +226,7 @@ bool CurlHttp::Post(const std::string& url, const std::string& postData, std::st
|
|
|
* @return true
|
|
|
* @return false
|
|
|
*/
|
|
|
-bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response)
|
|
|
+bool CurlHttp::Post(const std::string& url,const std::vector<std::string>& vecHeader, const std::string& postData, std::string& response)
|
|
|
{
|
|
|
CURL *curl;
|
|
|
CURLcode res;
|
|
@@ -177,13 +245,14 @@ bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string
|
|
|
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
|
|
|
|
|
|
/* 设置http头 */
|
|
|
+ struct curl_slist *headers = NULL;
|
|
|
if(!vecHeader.empty())
|
|
|
{
|
|
|
- struct curl_slist *headers = NULL;
|
|
|
for(auto &header : vecHeader)
|
|
|
{
|
|
|
headers = curl_slist_append(headers, header.c_str());
|
|
|
}
|
|
|
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
}
|
|
|
|
|
|
/* 设置包体 */
|
|
@@ -207,10 +276,11 @@ bool CurlHttp::PostWithBody(const std::string& url,const std::vector<std::string
|
|
|
bool result = true;
|
|
|
if(res != CURLE_OK)
|
|
|
{
|
|
|
- FMTLOG_ERROR("curl_easy_perform() failed: {}", curl_easy_strerror(res));
|
|
|
+ FMTLOG_ERROR("Post failed: {}, Url: {}", curl_easy_strerror(res), url);
|
|
|
result = false;
|
|
|
}
|
|
|
|
|
|
+ curl_slist_free_all(headers);
|
|
|
/* 清理curl */
|
|
|
curl_easy_cleanup(curl);
|
|
|
|