Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用libcurl通过https-linux,C下载文件?_C_Linux - Fatal编程技术网

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

如何使用libcurl通过https-linux,C下载文件?,c,linux,C,Linux,我正在尝试使用libcurl从本地https服务器下载文件。 然而,它没有做到这一点,我不知道如何调试这个? 佩罗尔没有设置任何东西。另外,我使用的本地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&

我正在尝试使用libcurl从本地https服务器下载文件。 然而,它没有做到这一点,我不知道如何调试这个? 佩罗尔没有设置任何东西。另外,我使用的本地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;
        }

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

您似乎忽略了curl\u easy\u perform()调用返回的状态。根据,这会让您非常清楚问题所在。此外,您还可以将curl\u easy\u setopt()与curl\u ERRORBUFFER一起使用(请参阅此以了解如何使用它)来确定curl发现了什么错误。我正在使用它从使用openssl的自签名证书下载一个文件。更新了我的代码,现在看到了真正的问题—SSL证书问题:自签名证书。知道我应该在这里做什么吗?理想情况下,我认为我们应该通过加载自签名证书来进行验证。
   ./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);