#pragma once#include <stdio.h> #include <stdlib.h> #include <curl/curl.h> #ifdef _debug#pragma Comment (lib, "Libcurld_imp.lib") #else #pragma comment (lib, "Libcurl_imp.lib") #endifclass Curlinit{public:curlinit () {Curl_global_init (Curl_global_all); } ~curlinit () {curl_global_cleanup (); } protected:static curlinit m_initialize;}; Curlinit Curlinit::m_initialize;class Cmyurl{public:cmyurl () {/* init the CURL session */Curl_handl E = Curl_easy_init (); } ~cmyurl () {/* Cleanup curl Stuff */curl_easy_cleanup (curl_handle); } public:int get_file (const char* szurl, const char* szfile) {/* set URL to get */Curl_easy_seto PT (Curl_handle, Curlopt_url, szurl); /* No progress meter Please */curl_easy_setopt (Curl_handle, curlopt_noprogress, 1L); /* Send all data to this function */curl_easy_setopt (Curl_handle, Curlopt_wriTefunction, Write_data); /* Open the files */file* Bodyfile = fopen (Szfile, "WB"); if (Bodyfile = = NULL) {curl_easy_cleanup (curl_handle); return-1; }/* We want the body is written to this file handle instead of STDOUT */curl_easy_setopt (Curl_hand Le, Curlopt_writedata, bodyfile); /* Get it! */Curl_easy_perform (curl_handle); /* Close the Body file */fclose (bodyfile); return 0; }protected:static size_t write_data (void *ptr, size_t size, size_t nmemb, void *stream) {int written = Fwri Te (ptr, Size, NMEMB, (FILE *) stream); return written; }protected:curl *curl_handle;};
Common code for C + +--download files using Libcurl