NoPaste

libcurl url2file

von ren22
Beschreibung:
libcurl url2file c code Beispiel
Snippet erstellt:
10.09.2019 02:07:08
Snippet wird automatisch gelöscht:
Niemals

Dein Code:
  1. /***************************************************************************
  2.  *                                  _   _ ____  _
  3.  *  Project                     ___| | | |  _ \| |
  4.  *                             / __| | | | |_) | |
  5.  *                            | (__| |_| |  _ <| |___
  6.  *                             \___|\___/|_| \_\_____|
  7.  *
  8.  * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
  9.  *
  10.  * This software is licensed as described in the file COPYING, which
  11.  * you should have received as part of this distribution. The terms
  12.  * are also available at https://curl.haxx.se/docs/copyright.html.
  13.  *
  14.  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15.  * copies of the Software, and permit persons to whom the Software is
  16.  * furnished to do so, under the terms of the COPYING file.
  17.  *
  18.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19.  * KIND, either express or implied.
  20.  *
  21.  ***************************************************************************/
  22. /* <DESC>
  23.  * Download a given URL into a local file named page.out.
  24.  * </DESC>
  25.  */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29.  
  30. #include <curl/curl.h>
  31.  
  32. /*static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
  33. {
  34.     size_t written = fwrite(ptr, size, nmemb, stream);
  35.     return written;
  36. }*/
  37.  
  38. int main(int argc, char *argv[])
  39. {
  40.     CURL *curl_handle;
  41.     CURLcode ret;
  42.     static const char *pagefilename = "page.out";
  43.     FILE *pagefile;
  44.     long response_code;
  45.  
  46.     if(argc < 2) {
  47.         printf("Usage: %s <URL>\n", argv[0]);
  48.         return 1;
  49.     }
  50.     curl_global_init(CURL_GLOBAL_ALL);
  51.     /* init the curl session */
  52.     curl_handle = curl_easy_init();
  53.     /* set URL to get here */
  54.     curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
  55.     /* Switch on full protocol/debug output while testing */
  56.     curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0L);
  57.     /* disable progress meter, set to 0L to enable and 1L disable progress meter */
  58.     curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);
  59.     /* send all data to this function  */
  60.     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
  61.     /* CURLOPT_FAILONERROR - request failure on HTTP response >= 400 */
  62.     curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1L);
  63.  
  64.     /* open the file */
  65.     pagefile = fopen(pagefilename, "wb");
  66.     if(!pagefile) {
  67.         curl_easy_cleanup(curl_handle);
  68.         fclose(pagefile);
  69.         return -1;
  70.     }
  71.  
  72.     ret = curl_easy_perform(curl_handle);
  73.  
  74.     /* get http error, if one */
  75.     if(ret == CURLE_HTTP_RETURNED_ERROR) {
  76.         curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
  77.     }
  78.     if(response_code == 404) {
  79.         printf("error 404\n");
  80.     }else{
  81.         //if(pagefile) {
  82.         /* write the page body to this file handle */
  83.         curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
  84.         /* get it! */
  85.         curl_easy_perform(curl_handle);
  86.         /* close the header file */
  87.         fclose(pagefile);
  88.  
  89.         /* cleanup curl stuff */
  90.         curl_easy_cleanup(curl_handle);
  91.         curl_global_cleanup();
  92.         }
  93.         return 0;
  94. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. Alternativ kannst du den gesamten Eintrag auch als Datei herunterladen.