SHARE
TWEET

Untitled

spliter93 Jul 22nd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #pragma comment(lib, "Normaliz.lib" )
  5. #pragma comment(lib, "wldap32.lib" )
  6. #pragma comment(lib, "crypt32.lib" )
  7. #pragma comment(lib, "Ws2_32.lib")
  8.  
  9. #define CURL_STATICLIB
  10. #include "curl/curl.h"
  11.  
  12. using namespace std;
  13.  
  14. size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  15.     size_t written = fwrite(ptr, size, nmemb, stream);
  16.     return written;
  17. }
  18.  
  19. void download(string url, string output)
  20. {
  21.     cout << "GET " << url << endl;
  22.  
  23.     // setup CURL
  24.     CURL *curl;
  25.     CURLcode res;
  26.     FILE *fp;
  27.  
  28.     curl = curl_easy_init();
  29.     if (curl)
  30.     {
  31.         // open
  32.         fp = fopen(output.c_str(), "wb");
  33.  
  34.         // set CURL options
  35.         curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  36.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  37.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  38.  
  39.         // perform request
  40.         res = curl_easy_perform(curl);
  41.  
  42.         // get elapsed time
  43.         double elapsed;
  44.         curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
  45.  
  46.         cout << "Downloaded in " << elapsed << " ms" << endl;
  47.  
  48.         // CURL cleanup
  49.         curl_easy_cleanup(curl);
  50.  
  51.         // close file
  52.         fclose(fp);
  53.  
  54.         return;
  55.     }
  56.  
  57.     cout << "CURL init error" << endl;
  58. }
  59.  
  60. int main(void)
  61. {
  62.     download(
  63.         "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
  64.         "google-logo.png"
  65.     );
  66.  
  67.     return 0;
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top