2012-03-19 180 views
3

我使用的是Visual Studio C++ 2010,它使用cURL工作得很好,但问题在于https请求不会返回任何内容。而不是显示输出:curl简单的https请求不会返回任何东西C++

#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; 
} 

这个代码,例如,它只是一个https请求谷歌,但它没有返回任何只是因为它以https开头。如果我拿走了https的“s”,它就可以正常工作:“http://www.google.com.br”正常显示结果。我在这里错过了什么?我正在使用cURL中的示例。 我尝试过与其他网站发生过同样的事情。 :/像https://www.facebook.com

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

在此先感谢。 :)

+0

是守业者SSL验证标志,真正零,你想要什么?有点扯皮似乎暗示它会绕过某种程度的验证。我的第一个尝试是尝试查看这些标志是否可以像使用https url卷动的命令行那样清除。我猜想它会以同样的方式失败。 – 2012-03-19 19:55:16

+0

迈克尔我不太确定。我想要的是在没有任何SSL安全性的情况下正确获取https,因为我希望它能够快速检索数据。但现在我什么都没有得到:/如果它危害数据的安全性,我希望获得最快的方式。 – Grego 2012-03-19 19:57:07

+0

你是从源代码构建的吗?如果是这样,你是否明确启用SSL支持?如果没有,您下载的软件包是否启用了SSL支持(或者,您是从哪里下载的)? – ildjarn 2012-03-19 20:01:19

回答

4

这个简单的例子对我的作品:

#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

+0

嗨fella,我正在使用Visual Studio C++ 2008上的Windows 7进行更具体的工作,我安装了OpenSSL,但是我通过Visual Studio 2008的Curl Page的教程构建了它,我不得不下载并安装openssl要做到这一点,但我正在做的这个应用程序我打算分发,我不得不要求大家安装OpenSSL?但首先,我想知道我在哪里告诉应用程序指向OpenSSL,我建立了它的支持,但我似乎没有得到它的工作,如果你有什么分享,我会很高兴。谢谢。 :) – Grego 2012-03-19 21:05:24

+0

哦是啊有没有办法忽略SSL,并使https甚至没有安全的事情只是检索HTML?因为我不关心安全性,我只想着从服务器接收数据 – Grego 2012-03-19 21:20:10

+0

谢谢。这在第二天就结束了我的地狱。 – 2013-12-17 16:01:04