libcurl 实现http post请求和http文件下载(C语言)

代码如下:

#include <stdio.h>
#include "curl/curl.h"
#include <unistd.h>

int http_postRequest(const char *data, const char *url, curl_write_callback write_callback)
{
  int ret = -1;
  CURL *curl = NULL;
  struct curl_slist *headers = NULL;

  curl = curl_easy_init();
  if (!curl)
  {
    printf("curl_easy_init failed.");
    goto label;
  }
  /*
   *   构建http报文头
   */
  headers = curl_slist_append(headers, "Content-Type:application/json; charset=UTF-8");
  if (headers == NULL)
  {
    printf("curl_slist_append failed.\n");
    goto label;
  }
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); // 是否验证服务器SSL证书的有效性 0:不验证 1:验证
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); // 是否检查服务器的SSL证书中的主机名与请求的主机名匹配 0:不检查 2:检查
  // curl_easy_setopt(curl, CURLOPT_CAINFO, ""); // 指定CA证书的位置
  curl_easy_setopt(curl, CURLOPT_URL, url);          // url地址
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);  // post数据
  // curl_easy_setopt(curl, CURLOPT_WRITEDATA, args);//传递给 write_callback() 回调函数的第四个参数
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); // 接受数据回调
  curl_easy_setopt(curl, CURLOPT_POST, 1);                       // post请求
  curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);                    // 是否打印调试信息
  curl_easy_setopt(curl, CURLOPT_HEADER, 0);                     // 是否输出响应头
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);                   // 设置整个cURL函数执行过程的最长等待时间,单位:秒
  ret = curl_easy_perform(curl);
  if (ret != CURLE_OK)
  {
    printf("curl error code: %d\n", ret);
  }

label:
  if (headers)
  {
    curl_slist_free_all(headers);
  }
  if (curl)
  {
    curl_easy_cleanup(curl);
  }

  return ret;
}

int progress_callback(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
  if (dltotal != 0)
  {
    printf("Download progress: %3.0f%%\n", (dlnow / (dltotal * 1.00)) * 100);
  }

  return 0;
}

int http_download(const char *url, const char *path)
{
  int ret = -1;
  FILE *fp = NULL;
  CURL *curl = NULL;

  fp = fopen(path, "wb");
  if (!fp)
  {
    printf("fopen failed.");
    goto label;
  }
  curl = curl_easy_init();
  if (!curl)
  {
    printf("curl_easy_init failed.");
    goto label;
  }
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  /*
   *   CURLOPT_WRITEFUNCTION 可以不用设置,curl会调用默认的回调函数,
   *   把数据写入fp所指向的文件中。
   */
  // curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  // curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, args); //传递给 progress_callback() 回调函数的第一个参数
  curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60 * 10);
  ret = curl_easy_perform(curl);
  if (ret != CURLE_OK)
  {
    printf("curl error code: %d\n", ret);
  }

label:
  if (curl)
  {
    curl_easy_cleanup(curl);
  }
  if (fp)
  {
    fclose(fp);
  }

  return ret;
}

size_t curl_recv_callback(char *buffer, size_t size, size_t nitems, void *outstream)
{
  printf("recv: %s\n", buffer);
  return size * nitems;
}

int main(void)
{
  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
  {
    printf("curl_global_init failed.");
    return -1;
  }
  // curl_version_info_data *version_info = curl_version_info(CURLVERSION_NOW);
  // printf("线程安全: %s\n", (version_info->features & CURL_VERSION_THREADSAFE) ? "enabled" : "disabled");
  printf("开始HTTP请求:\n");
  http_postRequest("", "www.baidu.com", curl_recv_callback);
  sleep(3);
  printf("开始文件下载:\n");
  http_download("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
                "./big_buck_bunny.mp4");
  curl_global_cleanup();

  return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以使用一些库来执行HTTP的GET和POST请求,并下载文件。在这里,我们将使用libcurl库来完成这些操作。 首先,我们需要在代码中引入libcurl库的头文件。 ```c #include <stdio.h> #include <curl/curl.h> ``` 然后,我们可以定义一个回调函数,用于处理下载的数据。 ```c size_t write_callback(void* ptr, size_t size, size_t nmemb, FILE* stream) { return fwrite(ptr, size, nmemb, stream); } ``` 接下来,我们可以编写一个函数来执行HTTP的GET请求,并将返回的数据保存到文件中。 ```c void http_get(const char* url, const char* file_path) { FILE* file = fopen(file_path, "wb"); if (file == NULL) { printf("无法打开文件!\n"); return; } CURL* curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { printf("请求失败:%s\n", curl_easy_strerror(res)); } curl_easy_cleanup(curl); } fclose(file); } ``` 最后,我们可以编写一个函数来执行HTTPPOST请求,并将返回的数据保存到文件中。 ```c void http_post(const char* url, const char* post_data, const char* file_path) { FILE* file = fopen(file_path, "wb"); if (file == NULL) { printf("无法打开文件!\n"); return; } CURL* curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { printf("请求失败:%s\n", curl_easy_strerror(res)); } curl_easy_cleanup(curl); } fclose(file); } ``` 通过使用以上两个函数,我们可以下载一个文件到指定的路径。例如,假设我们要下载一个名为"test.txt"的文件,可以使用以下代码: ```c http_get("http://example.com/test.txt", "test.txt"); ``` 或者,如果我们需要通过POST请求下载文件,可以使用以下代码: ```c const char* post_data = "param1=value1&param2=value2"; http_post("http://example.com/download", post_data, "test.txt"); ``` 以上就是使用C语言执行HTTP的GET和POST请求,并下载文件的示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值