使用libcurl:
size_t AppendDataToStringCurlCallback(void *ptr, size_t size, size_t nmemb, void *vstring)
{
std::string * pstring = (std::string*)vstring;
pstring->append((char*)ptr, size * nmemb);
return size * nmemb;
}
std::string DownloadUrlAsString(const std::string & url)
{
std::string body;
CURL *curl_handle;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, AppendDataToStringCurlCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &body);
curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);
return body;
}