【问题标题】:Saving a file using libcurl in C在 C 中使用 libcurl 保存文件
【发布时间】:2010-08-12 19:11:00
【问题描述】:

我正在从 perl 扩展到 C,我正在尝试使用 curl 的库来简单地从远程 url 保存文件,但我很难找到一个好的示例来工作。

另外,我不确定我应该使用 curl_easy_recv 还是 curl_easy_perform

【问题讨论】:

    标签: c libcurl http-get


    【解决方案1】:

    我发现 this resource 对开发人员非常友好。

    我编译了下面的源代码:

    gcc demo.c -o demo -I/usr/local/include -L/usr/local/lib -lcurl
    

    基本上,它会下载一个文件并将其保存在您的硬盘上。

    文件demo.c

    #include <curl/curl.h>
    #include <stdio.h>
    
    void get_page(const char* url, const char* file_name)
    {
      CURL* easyhandle = curl_easy_init();
    
      curl_easy_setopt( easyhandle, CURLOPT_URL, url ) ;
    
      FILE* file = fopen( file_name, "w");
    
      curl_easy_setopt( easyhandle, CURLOPT_WRITEDATA, file) ;
    
      curl_easy_perform( easyhandle );
    
      curl_easy_cleanup( easyhandle );
    
      fclose(file);
    
    }
    
    int main()
    {
      get_page( "http://blog.stackoverflow.com/wp-content/themes/zimpleza/style.css", "style.css" ) ;
    
      return 0;
    }
    

    另外,我相信您的问题与此类似:

    Download file using libcurl in C/C++

    【讨论】:

    • 每当我这样做时,我都会在我保存到的文件顶部获得标题。有没有办法解决这个问题? HTTP/1.1 200 OK Server: nginx/1.4.1 Date: Thu, 01 May 2014 16:56:21 GMT Content-Type: text/html; charset=utf-8 Content-Length: 7625 Connection: keep-alive
    • 没关系。我在这里找到了解决方案:stackoverflow.com/questions/5142869/…
    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多