#include "HttpClient.h" #include "Exceptions.h" #include #include #include namespace HttpClient { std::string UrlEncode(std::string url) { std::ostringstream ss; for (unsigned i=0; i(ptr); std::vector* vec = reinterpret_cast< std::vector* >(stream); vec->reserve( vec->size() + total); //allow vector to do a preallocation if necessary int i; for (i=0; ipush_back( bytes[i] ); } return i; } std::vector Get(std::string url) { std::vector buf; char errmsg[128] = ""; CURL* curl = curl_easy_init(); if (curl == 0) throw httpexception("could not init CURL"); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg); curl_easy_setopt(curl, CURLOPT_URL, url.c_str() ); curl_easy_setopt(curl, CURLOPT_FAILONERROR,1); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buf); int res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (res != 0) throw httpexception(errmsg); return buf; } std::string GetString(std::string url) { std::vector buf = Get(url); buf.push_back(0); return std::string(&buf[0]); } }