// // Created by matrixai on 2/10/23. // #include "Http.h" namespace ai_matrix { HttpUtil::HttpUtil() { pCurl_ = curl_easy_init(); } HttpUtil::~HttpUtil() { curl_easy_cleanup(pCurl_); curl_global_cleanup(); } size_t HttpUtil::callBack(void *pBuffer, size_t size, size_t nmemb, std::string &strResp) { size_t sizes = size * nmemb; std::string strTemp((char*)pBuffer, sizes); strResp += strTemp; return sizes; } /** * Post请求,使用Json传递请求参数 * @param requestUrl 请求地址 * @param json 请求参数 * @return 返回Json */ std::string HttpUtil::postRequestByJson(std::string strRequestUrl, Json::Value &json) { if (this->pCurl_ == nullptr) { pCurl_ = curl_easy_init(); if (nullptr == pCurl_) { LogError << "curl_easy_init failed !"; return "curl init failed"; } } // 设置请求类型 [POST,GET] curl_easy_setopt(pCurl_, CURLOPT_CUSTOMREQUEST, "POST"); // 设置请求地址 curl_easy_setopt(pCurl_, CURLOPT_URL, strRequestUrl.c_str()); // 设置请求超时时间(秒) curl_easy_setopt(pCurl_, CURLOPT_CONNECTTIMEOUT, 10); // 设置允许重定向(1-true,0-false) curl_easy_setopt(pCurl_, CURLOPT_FOLLOWLOCATION, 1); // URL不带协议的时候,使用的默认协议。 curl_easy_setopt(pCurl_, CURLOPT_DEFAULT_PROTOCOL, "https"); // 创建请求头 struct curl_slist *pHeaderList = nullptr; pHeaderList = curl_slist_append(pHeaderList, (std::string("blade-auth: ") + this->token_).c_str()); pHeaderList = curl_slist_append(pHeaderList, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"); pHeaderList = curl_slist_append(pHeaderList, "Accept:application/json"); pHeaderList = curl_slist_append(pHeaderList, "Content-Type:application/json"); pHeaderList = curl_slist_append(pHeaderList, "charset:utf-8"); curl_easy_setopt(pCurl_, CURLOPT_HTTPHEADER, pHeaderList); // 设置请求内容 Json::StreamWriterBuilder jswBuilder; std::string requestJson = Json::writeString(jswBuilder, json); curl_easy_setopt(pCurl_, CURLOPT_POSTFIELDS, requestJson.c_str()); // 设置回调函数(在回调函数里整理返回的数据) curl_easy_setopt(pCurl_, CURLOPT_WRITEFUNCTION, callBack); // 设置回调的接收变量 std::string strResponse; curl_easy_setopt(pCurl_, CURLOPT_WRITEDATA, &strResponse); // 执行请求 CURLcode res = curl_easy_perform(pCurl_); // 清除 headerlist curl_slist_free_all(pHeaderList); // 重置curl curl_easy_reset(pCurl_); if (res != CURLE_OK) { // LogError << " 请求失败,失败信息:" << curl_easy_strerror(res); return curl_easy_strerror(res); } return strResponse; } /** * post请求获取Token * @param user * @param password * @param tenantId * @return */ std::string HttpUtil::postRequestGetToken(std::string strRequestUrl, std::string strUsername, std::string strPassword, std::string strTenantId, std::string strAuthorization) { if (this->pCurl_ == nullptr) { pCurl_ = curl_easy_init(); if (nullptr == pCurl_) { LogError << "curl_easy_init failed !"; return "curl init failed"; } } // 设置请求类型 [POST,GET] curl_easy_setopt(pCurl_, CURLOPT_CUSTOMREQUEST, "POST"); // 设置请求地址 curl_easy_setopt(pCurl_, CURLOPT_URL, strRequestUrl.c_str()); // 设置请求超时时间(秒) curl_easy_setopt(pCurl_, CURLOPT_CONNECTTIMEOUT, 10); // 设置允许重定向(1-true,0-false) curl_easy_setopt(pCurl_, CURLOPT_FOLLOWLOCATION, 1); // URL不带协议的时候,使用的默认协议。 curl_easy_setopt(pCurl_, CURLOPT_DEFAULT_PROTOCOL, "https"); // 设置请求头 struct curl_slist *pHeaderList = NULL; pHeaderList = curl_slist_append(pHeaderList, "Authorization: "); pHeaderList = curl_slist_append(pHeaderList, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"); curl_easy_setopt(pCurl_, CURLOPT_HTTPHEADER, pHeaderList); // 设置请求参数 curl_mime *mime; curl_mimepart *part; mime = curl_mime_init(pCurl_); part = curl_mime_addpart(mime); curl_mime_name(part, "username"); curl_mime_data(part, strUsername.c_str(), CURL_ZERO_TERMINATED); part = curl_mime_addpart(mime); curl_mime_name(part, "password"); curl_mime_data(part, strPassword.c_str(), CURL_ZERO_TERMINATED); part = curl_mime_addpart(mime); curl_mime_name(part, "tenantId"); curl_mime_data(part, strTenantId.c_str(), CURL_ZERO_TERMINATED); part = curl_mime_addpart(mime); curl_mime_name(part, "grant_type"); curl_mime_data(part, "password", CURL_ZERO_TERMINATED); curl_easy_setopt(pCurl_, CURLOPT_MIMEPOST, mime); // 执行请求 CURLcode res = curl_easy_perform(pCurl_); // 清除 headerlist curl_slist_free_all(pHeaderList); // 重置curl curl_easy_reset(pCurl_); if (res != CURLE_OK) { // LogError << " 请求失败,失败信息:" << curl_easy_strerror(res); return curl_easy_strerror(res); } return "strResponse"; } }