一、使用准备条件,添加文件头#include <curl/curl.h> ,在makefile文件中添加库lcurl
二、写代码
size_t writedata(char *ptr, size_t size, size_t nmemb, string * page)
{
if(page == NULL)
{
return 0;
}
page->append(ptr, size*nmemb);
return size*nmemb;
}
//发送post的http消息
int sendpostrequest()
{
CURL* curl;
CURLcode code;
curl = curl_easy_init();
string response;
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http:xxxxxxxxxxxxxxxx");
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); //设置超时时间
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); //屏蔽其它信号
code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
if (code != CURLE_OK)
{
return -1;
}
code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (code != CURLE_OK)
{
return -1;
}
} else {
return -1;
}
}