Initial commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#include "httpClient.h"
|
||||
|
||||
|
||||
httpClient::httpClient()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
m_userAgents.push_back("Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00");
|
||||
|
||||
initialize_locks();
|
||||
}
|
||||
|
||||
httpClient::~httpClient()
|
||||
{
|
||||
free_locks();
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
std::string httpClient::GetUrlContents(const std::string& url) const
|
||||
{
|
||||
std::string buffer;
|
||||
|
||||
CURL* curl = curl_easy_init();
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
|
||||
int code = 0;
|
||||
curl_easy_perform(curl);
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if (code != 200)
|
||||
{
|
||||
std::stringstream error;
|
||||
error << "Error encountered while retrieving " << url;
|
||||
throw std::runtime_error(error.str());
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
size_t httpClient::WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData)
|
||||
{
|
||||
if (writerData == NULL)
|
||||
return 0;
|
||||
|
||||
writerData->append(data, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user