visual-c++ - 通过 curl 和 Visual C++ 2010 的 HTTP+SSL

标签 visual-c++ ssl curl

我有一个自签名证书和基于 apache 2.x 的网站,带有此证书。 只想通过 SSL 从我的软件访问此站点。

执行以下操作:

int _tmain(int argc, _TCHAR* argv[])
{

CURL *curl;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://mysite.com");
    curl_easy_setopt(curl, CURLOPT_CAINFO, "n.crt");
 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_CAPATH, "D:\\");
 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L) ;
 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT); 

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }

  curl_global_cleanup();

    return 0;
}

但每次我得到:

* About to connect() to mysite.com port 443 (#0)
*   Trying 1.1.1.1... * connected
* Connected to mysite.com (1.1.1.1) port 443 (#0)
* error setting certificate verify locations:
  CAfile: n.crt
  CApath: D:\

* Closing connection #0
* Problem with the SSL CA cert (path? access rights?)
curl_easy_perform() failed: Problem with the SSL CA cert (path? access rights?)
Press any key to continue . . .

请问我做错了什么?

附言:

  • Win7 x64
  • MSVC++ 2010
  • curl 7.19.3 (i386-pc-win32) libcurl/7.19.3 OpenSSL/0.9.8j 协议(protocol):tftp ftp telnet dict ldap http 文件 https ftps

最佳答案

找到n.crt的位置。当可执行文件运行时,所有文件检查都是相对于它的路径的。因此,要么将 n.crt 放在可执行文件的文件夹中,要么在设置 CURLOPT_CAINFO 时放置文件的绝对路径。否则使用证书信息文件的相对路径。

关于visual-c++ - 通过 curl 和 Visual C++ 2010 的 HTTP+SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487529/

相关文章:

c++ - 将属性表应用到空的 C++ Visual Studio 项目

c++ - Nicolai Josuttis 在他的书中说,开放成员函数不会清除状态标志。这不是我在 VS2010 中发现的。这是微软的问题吗?

c++ - DllMain 应该命名为另一个名称吗?

Apache - 在没有安全警报的情况下将 https 重定向到不同的域

PHP curl 返回 (35) : SSL connect error

bash - curl -C, --continue-at 在管道标准输出时工作吗?

php - 如何避免使用 PHP cURL 进行 URL 通配?

c++ - 在 C++ 的 if 语句中使用数组中的特定值

java - 出站连接上的客户端证书

php - cURL 加载时间过长