c - 如何使用 libcurl 通过 https 下载文件 - linux,C?

标签 c linux

我正在尝试使用 libcurl 从本地 https 服务器下载文件。 但是,它没有这样做,我不知道如何调试这个? perror 没有设置任何内容。另外,我正在使用的本地 https 服务器正在使用 - openssl - 2048 位。这是使用以下命令生成的。

openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365

这是我的 C 代码。我在 linux 上运行这个 - C

    #include <openssl/err.h>
    #include <openssl/ssl.h>
    #include <curl/curl.h>
    #include <stdio.h>

    size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
    {
      printf("called.. writeFunction\r\n");
      fwrite(ptr, size, nmemb, (FILE *)stream);
      return (nmemb*size);
    }

        int main(void)
        {
          CURL *ch;
          CURLcode rv;
          char caPath[128];
          char errbuf[CURL_ERROR_SIZE];

          rv = curl_global_init(CURL_GLOBAL_ALL);
          ch = curl_easy_init();


          rv = curl_easy_setopt(ch, CURLOPT_URL, "https://110.166.10.296:9000/test.conf");

         /* provide a buffer to store errors in */
  curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, errbuf);
           /* provide a buffer to store errors in */
  curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, errbuf);

          rv = curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
          rv = curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
          rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);

          printf("set Up CA Path..\r\n");
          memset(caPath,'0',sizeof(caPath));
          strcpy(caPath,"/home/test/SSL_Server");
          rv = curl_easy_setopt(ch, CURLOPT_CAPATH,caPath);

          rv = curl_easy_perform(ch);
          printf("curl easy perform done..\r\n");

          if(rv == CURLE_OK)
            printf("*** transfer succeeded ***\n");
          else
          {
             printf("*** transfer failed..****\n");
             perror("failed:");

                   /* if the request did not complete correctly, show the error
  information. if no detailed error information was written to errbuf
  show the more generic information from curl_easy_strerror instead.
  */
    size_t len = strlen(errbuf);
    fprintf(stderr, "\nlibcurl: (%d) ", rv);
    if(len)
      fprintf(stderr, "%s%s", errbuf,
              ((errbuf[len - 1] != '\n') ? "\n" : ""));
    else
      fprintf(stderr, "%s\n", curl_easy_strerror(rv));
          }


          return 0;
        }

编译 -

gcccurl.c -ocurl.out-lcurl

O/p -

   ./curl.out 
set Up CA Path..
curl easy perform done..
*** transfer failed..****

libcurl: (60) SSL certificate problem: self signed certificate

最佳答案

rv = curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);

此代码正在启用​​ SSL 证书验证,但由于您使用的是自签名证书而失败。将此选项设置为 0 以禁用验证。

关于c - 如何使用 libcurl 通过 https 下载文件 - linux,C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49120878/

相关文章:

对文件 I/O 的困惑 (C)

多核机器上的 OpenSSL

c - fscanf() 仅写入输入文件中的部分内容而不是全部内容

javascript - WebAssembly emsdk 安装失败

python - 为什么 Python 升级到 3.8 后 Pip 无法运行?

Linux静态代码分析工具对比?

linux - 如何删除文件名开头的数字、破折号和下划线

c - 函数内是否可以获取由返回值初始化的变量的内存地址?

c - 在多个 cmocka 单元测试中分配内存

linux - insmod 和 modprobe 有什么区别?