c - 在 C 中使用 libcurl 保存文件

标签 c libcurl http-get

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

此外,我不确定我是否应该使用 curl_easy_recv 或 curl_easy_perform

最佳答案

我找到了 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++

关于c - 在 C 中使用 libcurl 保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3471122/

相关文章:

c - 在 C 中使用 HTTP 请求获取图像

java - Java 中的 Apache HttpClient,instream.toString = org.apache.http.conn.EofSensorInputStream

从 VB 6.0 dll 调用 win 32 C 库函数崩溃

c - 二进制添加大型字符数组?

c++ - libcurl C++ 添加 http get 参数

c - 使用 C 的 Libcurl

c++ - 我需要清理 curl_easy_strerror 缓冲区吗?

c - 函数已定义但未在 C 中使用警告

c++ - 旋转矩阵的方向 vector

javascript - Jquery向PHP发送数据并接收数据