http://blog.csdn.net/flyfish1986/article/details/50896271 2016
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
main函數(shù)
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.");
struct curl_slist *headers = NULL;
//增加HTTP header
headers = curl_slist_append(headers, "Accept:application/json");
headers = curl_slist_append(headers, "Content-Type:application/json");
headers = curl_slist_append(headers, "charset:utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "post的內(nèi)容");
static const char *pagefilename = "respone.txt";
FILE *pagefile;
//寫入到文件
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
fopen_s(&pagefile,pagefilename, "wb");
if (pagefile)
{
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pagefile);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
}
fclose(pagefile);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
|