接口释义
使用libcurl实现http通信——get获取response
代码实现
size_t responseStr(void* ptr, size_t size, size_t nmemb, void *userdata) {
if (ptr == NULL || userdata == NULL || size == 0) {
return 0;
}
size_t realSize = size*nmemb;
std::string *str = (std::string*)userdata;
(*str).append((char*)ptr, realSize);
return realSize;
}
void postHttp() {
std::string data;
CURL *curl;
long status;
struct curl_slist* headers = NULL;
std::string str = "test";
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "https://*************");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, str.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, str.size());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseStr);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&data);
CURLcode res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
curl_easy_cleanup(curl);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)