使用CURL从HTTPS连接中获取内容 [英] Getting no content from a HTTPS connection using CURL

查看:276
本文介绍了使用CURL从HTTPS连接中获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图最近了解更多关于CURL,并终于设法编译和正确地安装它作为我的测试项目的静态库。



我已经成功地管理连接并打印出从 http: //www.google.se



连线至安全的http网页 https://www.google.com .se 我获得一个空字符串作为页面内容。



我正在使用可以获取有关选项的信息。



我已经尝试过这个回答,但它没有工作或我做错了。

我也试过关闭verifypeer和verifyhost(虽然我真的想练习安全的解决方案),但它也没有工作。



我需要做什么来使它工作?
我做错了什么?




$ b

这里是测试代码

$ b

  #include< iostream> 
#include< string>
#include< curl / curl.h>

using namespace std;

static size_t WriteCallback(void * contents,size_t size,size_t nmemb,void * userp){
((string *)userp) - > append * nmemb);
return size * nmemb;
}

int main(){
CURL * curl;
CURLcode res;
string readBuffer;

curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl,CURLOPT_URL,https://www.google.se);
// curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,TRUE);

//似乎不工作
curl_easy_setopt(curl,CURLOPT_CAINFO,path\\\cacert.pem);

//这不是
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,FALSE);

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteCallback);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,& readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

cout<< readBuffer<< endl;

系统(pause);
}
return 0;
}






更新



所以我得到了来自curl的错误消息,说:不支持的协议,我猜这是SSL不说工作。所以我不得不重新编译它与SSL(这是奇怪,因为我以为我第一次),但... ...



我几乎要放弃。叹。出于某种原因或其他原因,现在它给我的错误
NMAKE:致命错误U1077 nasmw 制作SSL时,即使我明确给它的权利%PATH %到nasm。我按照步骤来写信。



所以我试着使用 curl二进制类型libcurl ,但我不知道如何在VC ++中正确链接,因为库文件对我不熟悉。



我在尝试编译测试项目时仍然得到这些链接器错误。

  1> main.obj:error LNK2019:未解析的外部符号_curl_easy_cleanup在函数_main中引用
1> main.obj:error LNK2019:未解析的外部符号_curl_easy_strerror在函数_main中引用
1> main.obj:error LNK2019:未解析的外部符号_curl_easy_perform在函数_main中引用
1> main.obj:error LNK2019:未解析的外部符号_curl_easy_setopt在函数_main中引用
1> main.obj:error LNK2019 :未解析的外部符号_curl_easy_init在函数_main中引用

很沮丧...我希望我理解为什么它非常复杂。

我只是想要使用库!






更新2



好吧...我设法使用SSL编译curl库并报告 CURL_VERSION_SSL 已启用

  curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW); 

if(vinfo-> features& CURL_VERSION_SSL){
cout<<CURL:SSL enabled<< endl;
} else {
cout<<CURL:SSL未启用<< endl;
}

//打印出CURL:SSL enabled

但我仍然得到相同的错误消息不支持的协议。我不知道我做错了什么。

解决方案

好吧,在我编译库第n次,一切似乎很好,直到我调试它,它给了我堆错误,提示我犯了一个错误*抽动抽搐*。



在开始新的编译会话之前,我再次查看curl.haxx,发现了一个MSVC包。它让我高兴,因为我知道如何处理这些!我选择了静态库,因为这是我想要的。



虽然,不是一切顺利,我不得不添加一些额外的链接器依赖关系,以正常工作。 p>

  #pragma comment(lib,curllib_static.lib) 
#pragma注释(lib,wldap32.lib)
#pragma注释(lib,ws2_32.lib)
#pragma注释(lib,winmm.lib)
#pragma comment(lib,ssleay32.lib)
#pragma注释(lib,openldap.lib)
#pragma注释(lib,libeay32.lib)

将其放入并使用我下载的其他包中的证书包,以便我可以连接到HTTPS。

  curl_easy_setopt(curl,CURLOPT_CAINFO,_path_\ca-bundle.crt); 

我收到了一些警告,我从来没有见过。他们不会混淆任何东西,但它是与PDB文件vc90,我认为VC ++本身处理的东西。



这里的代码连接到 之后, https://www.google.se (pragmas已删除并在项目属性下的链接器依赖项中添加)具有适当的CURL库。



这是 MSVC软件包(启用SSL)。您可以使用

如果您使用静态库,例如我必须添加预处理器定义 CURL_STATICLIB

  #include< ; iostream> 
#include< string>
#include< curl / curl.h>

using namespace std;

static size_t WriteCallback(void * contents,size_t size,size_t nmemb,void * userp){
((string *)userp) - > append * nmemb);
return size * nmemb;
}

int main(){
CURL * curl;
CURLcode res;
string readBuffer;

curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl,CURLOPT_URL,https://www.google.se);
curl_easy_setopt(curl,CURLOPT_CAINFO,C:\\libcurl_static\\ca-bundle.crt);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteCallback);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,& readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

cout<< readBuffer<< endl;

系统(pause);
}
return 0;
}

真的是我的错,我应该检查下载向导他们有。我只下载档案库(7.27.0)的最新来源,因为我认为 自行编译以获得我想要的。



这整个东西很尴尬。我从中学到了很多。


I've been trying to learn more about CURL recently and finally managed to compile and install it properly as a static library for my test project. Eventually I'll move on to learning about posting forms and such.

I've successfully managed to connect and print out page content from http://www.google.se.

When connecting to a secure http page https://www.google.se I get an empty string as page content.

I'm using this to get information about the options.

I've tried the things from this answer, but it didn't work or I did it wrong.
I also tried turning off verifypeer and verifyhost (though I really want to practice safe solutions), but it didn't work either.

What do I need to do to make it work? Am I doing something wrong?


Here's the test code

#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
    ((string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(){
  CURL *curl;
  CURLcode res;
  string readBuffer;

  curl = curl_easy_init();
  if(curl){
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.se");
    //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE);

    //Doesn't seem to work
    curl_easy_setopt(curl, CURLOPT_CAINFO, "path\\cacert.pem");

    //Neither does this
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    cout<<readBuffer<<endl;

    system("pause");
  }
  return 0;
}


Update

So I got the error message from curl saying Unsupported protocol, which I'm guessing is what it says when SSL doesn't work. So I had to recompile it with SSL (which is odd, because I thought I did the first time) but...

I'm almost about to give up. Sigh. For some reason or other, now it gave me the error NMAKE: fatal error U1077 nasmw when making the SSL, even though I clearly gave it the right %PATH% to nasm. I followed the steps to the letter.

So I tried using the curl binaries of type libcurl, but I don't know how to link it properly in VC++ because the library files are unfamiliar to me.

I keep getting these linker errors when trying to compile the test project.

1>main.obj : error LNK2019: unresolved external symbol _curl_easy_cleanup referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_strerror referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_perform referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_setopt referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_init referenced in function _main

So frustrated... I wish I understood why it has to be so complicated.
I just want to use the library already!


Update 2

Ok... I managed to compile the curl library with SSL and reporting CURL_VERSION_SSL is enabled

curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);

if(vinfo->features & CURL_VERSION_SSL){
    cout<<"CURL: SSL enabled"<<endl;
}else{
    cout<<"CURL: SSL not enabled"<<endl;
}

//Prints out "CURL: SSL enabled"

but I'm still getting the same error message Unsupported protocol. I don't know what I'm doing wrong.

解决方案

Alright, after I'd compiled the library for the nth time, everything seemed fine until I debugged it and it gave me heap errors, suggesting I made a mistake *twitch twitch* again.

Before starting a new compiling session I took another look around the curl.haxx and found an MSVC package. It made me gleeful, because I know how to handle those! I chose the static library, because that's what I wanted.

Though, not everything went smoothly, I had to add some additional linker dependencies for it to work properly.

namely

#pragma comment(lib, "curllib_static.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "ssleay32.lib")
#pragma comment(lib, "openldap.lib")
#pragma comment(lib, "libeay32.lib")

Put those in and used the certificate bundle from the other package I downloaded so that I could connect to HTTPS.

curl_easy_setopt(curl, CURLOPT_CAINFO, "_path_\ca-bundle.crt");

I am getting a few warnings though, warnings I've never seen before. They don't mess anything up, but it's got something to do with the PDB file "vc90", something I thought VC++ handled by itself.

Here's the code connecting to https://www.google.se (pragmas removed and added in linker dependencies under project properties) after finally having a proper CURL library.

Here's the MSVC package (SSL Enabled). You can use one of the scripts (perl or VBscript) from this package to make the certificate bundle for you.

Oh, also, if you're using a static library like me, you have to add the preprocessor definition CURL_STATICLIB

#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
    ((string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(){
  CURL *curl;
  CURLcode res;
  string readBuffer;

  curl = curl_easy_init();
  if(curl){
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.se");
    curl_easy_setopt(curl, CURLOPT_CAINFO, "C:\\libcurl_static\\ca-bundle.crt");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    cout<<readBuffer<<endl;

    system("pause");
  }
  return 0;
}

Was really my fault, I should've checked the description on the download wizard they have. I only downloaded the latest source from the archives (7.27.0) because I thought I had to compile it myself to get what I wanted.

This whole thing is embarrassing. I learned a lot from it though.

这篇关于使用CURL从HTTPS连接中获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆