forked from markus/S_New4
add download to file function for curl
This commit is contained in:
parent
b1d4aaee17
commit
0bda3b6c4c
@ -26,6 +26,7 @@ void PageManager::setDebugMode(bool status)
|
|||||||
this->debugMode = status;
|
this->debugMode = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Save reply to string
|
||||||
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||||
{
|
{
|
||||||
//Function für CURL
|
//Function für CURL
|
||||||
@ -33,6 +34,13 @@ size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Write data to file
|
||||||
|
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
|
||||||
|
{
|
||||||
|
size_t written = fwrite(ptr, size, nmemb, reinterpret_cast<FILE *>(stream));
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
|
||||||
Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::string data, bool generateCookieFile)
|
Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::string data, bool generateCookieFile)
|
||||||
{
|
{
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
@ -98,6 +106,68 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin
|
|||||||
return Reply(readBuffer, returnUrl);
|
return Reply(readBuffer, returnUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int PageManager::downLoadToFile(std::string filePath, std::string url)
|
||||||
|
{
|
||||||
|
CURL *curl_handle;
|
||||||
|
FILE *pagefile;
|
||||||
|
CURLcode res;
|
||||||
|
|
||||||
|
/* open the file */
|
||||||
|
pagefile = fopen(filePath.c_str(), "wb"); // w == write; b == binäre
|
||||||
|
if(!pagefile) {
|
||||||
|
perror("Open File filed");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* init the curl session */
|
||||||
|
curl_handle = curl_easy_init();
|
||||||
|
if(!curl_handle) {
|
||||||
|
perror("\33[2K\r => Error: Curl easy init failed");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set URL to get here */
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
|
||||||
|
|
||||||
|
/* Switch on full protocol/debug output while testing */
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, false);
|
||||||
|
|
||||||
|
/* disable progress meter, set to 0L to enable and disable debug output */
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, true);
|
||||||
|
|
||||||
|
/* send all data to this function */
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
|
|
||||||
|
//Sock5Proxy für Curl
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_PROXY, sock5Proxy.c_str() );
|
||||||
|
|
||||||
|
//User Agent
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0");
|
||||||
|
|
||||||
|
/* write the page body to this file handle */
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
|
||||||
|
|
||||||
|
|
||||||
|
/* get it! */
|
||||||
|
if( (res = curl_easy_perform(curl_handle)) != CURLE_OK) {
|
||||||
|
perror((std::string("\33[2K\r => Error: curl_easy_perform() failed: ") + curl_easy_strerror(res)).c_str());
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* close the header file */
|
||||||
|
if(fclose(pagefile) != 0) {
|
||||||
|
perror(" => Error: fclose failed");
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* cleanup curl stuff */
|
||||||
|
curl_easy_cleanup(curl_handle);
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int PageManager::login(Account account)
|
int PageManager::login(Account account)
|
||||||
{
|
{
|
||||||
if(debugMode)
|
if(debugMode)
|
||||||
|
@ -30,6 +30,8 @@ public:
|
|||||||
void setDebugMode(bool status);
|
void setDebugMode(bool status);
|
||||||
|
|
||||||
Reply getServerRequest(std::string Url, bool useCookies = false, std::string data = "", bool generateCookieFile = false);
|
Reply getServerRequest(std::string Url, bool useCookies = false, std::string data = "", bool generateCookieFile = false);
|
||||||
|
int downLoadToFile(std::string filePath, std::string url);
|
||||||
|
|
||||||
int login(Account account);
|
int login(Account account);
|
||||||
std::string getUrlAfterRedirect(std::string Url);
|
std::string getUrlAfterRedirect(std::string Url);
|
||||||
std::string checkName(std::string Name);
|
std::string checkName(std::string Name);
|
||||||
|
Loading…
Reference in New Issue
Block a user