Guest User

httpdownload.c

a guest
Jul 1st, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include <curl/curl.h>
  6.  
  7. int main( int argc, char * argv[] )
  8. {
  9.     CURL * curl;
  10.     CURLcode res;
  11.     FILE * pf;
  12.  
  13.     char * myurl = argv[1];
  14.     char * output = argv[2];
  15.  
  16.     curl = curl_easy_init();
  17.  
  18.     if(!curl)
  19.     {
  20.         fprintf(stderr, "Fatal: curl_easy_init() error.\n");
  21.         return EXIT_FAILURE;
  22.     }
  23.  
  24.     pf = fopen( output, "wb" );
  25.  
  26.     if(!pf)
  27.     {
  28.         fprintf(stderr, "Fatal: cannot open output file for writing: %s\n", output );
  29.  
  30.         curl_easy_cleanup( curl );
  31.         return EXIT_FAILURE;
  32.     }
  33.  
  34.     curl_easy_setopt(curl, CURLOPT_URL, myurl );
  35.     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L );
  36.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite );
  37.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, pf );
  38.  
  39.     res = curl_easy_perform(curl);
  40.  
  41.     if( res != CURLE_OK )
  42.     {
  43.         fprintf( stderr, "Request failed: curl_easy_perform(): %s\n", curl_easy_strerror(res) );
  44.  
  45.         curl_easy_cleanup( curl );
  46.         fclose(pf);
  47.         return EXIT_FAILURE;
  48.     }
  49.  
  50.     fprintf( stderr, "Success: file %s successfully written!\n", output );
  51.  
  52.     curl_easy_cleanup( curl );
  53.     fclose(pf);
  54.     return EXIT_SUCCESS;
  55. }
  56.  
  57. /* eof */
Add Comment
Please, Sign In to add comment