Curl简单的https请求不返回任何C ++

时间:2012-03-19 19:49:04

标签: c++ curl

我正在使用Visual Studio C ++ 2010,它与cURL的工作正常,但问题是https请求什么都不返回。而不是显示输出:

#include "StdAfx.h"
#include <stdio.h>
#include <curl/curl.h>
#include <conio.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  _getch();
  return 0;
}

例如,此代码只是对Google的https请求,但它不会返回任何内容,因为它以https开头。如果我拿走https的“s”,它就可以正常工作:“http://www.google.com.br”通常会显示结果。我在这里想念的是什么?我正在使用cURL中的示例。 我尝试过与其他网站同样的事情发生了。 :/ like https://www.facebook.com

顺便说一句,如果你们知道如何将网页内容存储在一个字符串中,我很高兴知道。

提前致谢。 :)

1 个答案:

答案 0 :(得分:5)

这个简单的例子对我有用:

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

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.co.uk");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

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

我很久以前得到了源代码并构建了库,但我确定在编译源代码之前启用了SSL支持。确保安装了OpenSSL并且(如果您在Linux下工作),您可以正确初始化PKG_CONFIG_PATH变量。这些是您在执行configure脚本时指定的两个选项。

  --with-ssl=PATH         Where to look for OpenSSL, PATH points to the SSL
                          installation (default: /usr/local/ssl); when
                          possible, set the PKG_CONFIG_PATH environment
                          variable instead of using this option

  --without-ssl           disable OpenSSL

我希望它有所帮助。

如果您使用的是Windows,这篇文章对您也很有用:

Building libcurl with SSL support on Windows

相关问题