原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/2019/0713/574.html
size_t http_data_writer(void* data, size_t size, size_t nmemb, void* content)
{
long totalSize = size*nmemb;
std::string* symbolBuffer = (std::string*)content;
if(symbolBuffer)
{
symbolBuffer->append((char *)data, ((char*)data)+totalSize);
}
return totalSize;
}
bool AccessWeb(char* szUrl)
{
CURL* curl = NULL;
curl=curl_easy_init();
CURLcode code;
// 设置Url
code = curl_easy_setopt(curl, CURLOPT_URL, szUrl);
// 设置post的json数据
char szPost[64] = "msg=hello";
code = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szPost);
// 设置回调函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_func);
//设置写数据
std::string strData;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strData);
// 执行请求
code = curl_easy_perform(curl);
if(code == CURLcode::CURLE_OK)
{
long responseCode = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
if (responseCode < 200 || responseCode >= 300 || strData.empty())
{
return false;
}
//下面可以对应答的数据进行处理了
// strData
}
// 清除curl对象
curl_easy_cleanup(curl);
return true;
}
上篇:上一篇:C++多线程技术windows常用办法
下篇:下一篇:windows多线程关键段 CriticalSection