MoreNotepad++

--------活出自己的精彩。

导航

libcurl 学习(0)

libcurl first sample

#include <iostream>
#include <typeinfo>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <curl/curl.h>

using namespace std;

/*
*  size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
*  curl_global_init(CURL_GLOBAL_WIN32);
* CURL *easy_handle = curl_easy_init(); * curl_easy_setopt(easy_handle, CURLOPT_URL, "
http://www.baidu.com/img/baidu.gif"); * curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); * curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &data); * curl_easy_perform(easy_handle);
* curl_easy_cleanup(easy_handle);
* curl_global_cleanup();
*/ size_t /* CALLBACK */ write_data(void *buffer, size_t size, size_t nmemb, void *userp) { string &data = *((string*)userp); data.append((char*)buffer, size * nmemb); return size * nmemb; } int main() { CURLcode retCode; retCode = curl_global_init(CURL_GLOBAL_WIN32); if (retCode != CURLE_OK) { cout << "curl_global_init error" << endl; return retCode; } /* curl_version_info_data *infodata = curl_version_info(CURLVERSION_NOW); */ CURL *easy_handle = curl_easy_init(); curl_easy_setopt(easy_handle, CURLOPT_URL, "http://www.baidu.com/img/baidu.gif"); string data; curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &data); curl_easy_perform(easy_handle); FILE * file = fopen("baidu.gif","wb"); fseek(file, 0, SEEK_SET); fwrite(data.c_str(), 1, data.length(), file); fclose(file);
curl_easy_cleanup(
easy_handle);
curl_global_cleanup();
return0;
}

 

posted on 2013-06-28 13:35  MoreNotepad++  阅读(221)  评论(0)    收藏  举报