bool HTTPClient::GETBinaryToFile(const std::string &url, const std::string &outputfile) { try { if (!CheckIfGlobalInitDone()) return false; //open the output file for writing std::ofstream outfile; outfile.open(outputfile.c_str(),std::ios::out|std::ios::binary|std::ios::trunc); if (!outfile.is_open()) return false; CURL *curl=curl_easy_init(); if (!curl) return false; CURLcode res; SetGlobalOptions(curl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_curl_data_file); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&outfile); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); outfile.close(); return (res==CURLE_OK); } catch (...) { return false; } }
bool HTTPClient::POSTBinary(const std::string &url, const std::string &postdata, const std::vector<std::string> &ExtraHeaders, std::vector<unsigned char> &response, const bool bFollowRedirect) { try { if (!CheckIfGlobalInitDone()) return false; CURL *curl=curl_easy_init(); if (!curl) return false; CURLcode res; SetGlobalOptions(curl); if (!bFollowRedirect) { curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L); } curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1); struct curl_slist *headers=NULL; if (ExtraHeaders.size()>0) { std::vector<std::string>::const_iterator itt; for (itt=ExtraHeaders.begin(); itt!=ExtraHeaders.end(); ++itt) { headers = curl_slist_append(headers, (*itt).c_str()); } } if (headers!=NULL) { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); } curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (headers!=NULL) { curl_slist_free_all(headers); /* free the header list */ } return (res==CURLE_OK); } catch (...) { return false; } }
bool HTTPClient::GETBinary(const std::string url, std::vector<unsigned char> &response) { if (!CheckIfGlobalInitDone()) return false; CURL *curl=curl_easy_init(); if (!curl) return false; CURLcode res; SetGlobalOptions(curl); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); return (res==CURLE_OK); }
bool HTTPClient::GETBinary(const std::string url, const std::vector<std::string> ExtraHeaders, std::vector<unsigned char> &response, const int TimeOut) { try { if (!CheckIfGlobalInitDone()) return false; CURL *curl=curl_easy_init(); if (!curl) return false; CURLcode res; SetGlobalOptions(curl); if (TimeOut != -1) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, TimeOut); } struct curl_slist *headers=NULL; if (ExtraHeaders.size()>0) { std::vector<std::string>::const_iterator itt; for (itt=ExtraHeaders.begin(); itt!=ExtraHeaders.end(); ++itt) { headers = curl_slist_append(headers, (*itt).c_str()); } } if (headers!=NULL) { curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); } curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); if (headers!=NULL) { curl_slist_free_all(headers); /* free the header list */ } return (res==CURLE_OK); } catch (...) { return false; } }