Chinaunix首页 | 论坛 | 博客
  • 博客访问: 641794
  • 博文数量: 63
  • 博客积分: 1265
  • 博客等级: 中尉
  • 技术积分: 789
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-06 21:54
文章分类

全部博文(63)

文章存档

2017年(1)

2016年(3)

2015年(2)

2013年(5)

2012年(20)

2011年(32)

分类: Html/Css

2017-12-20 09:15:29

  执行curl_easy_perform()的时候,返回错误代码:CURLE_UNSUPPORTED_PROTOCOL(1),同时通过打印日志会得到错误提示:Protocol https not supported or disabled in libcurl"。意思是:不支持HTTPS协议!有人说添加下面两行代码就可以解决:

        curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,false);//设定为不验证证书和HOST

        curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,false);

        测试发现,还是不能支持HTTPS访问!此时,如果还是无法访问HTTPS的网页的话,问题可能出在libcurl本身,很可能是 libcurl 在编译的时候没有选择支持SSL,所以还是重新编译一次libcurl吧!这里要提醒大家的是,通过用CMake工具生成的sln文件来直接编译lib库(我之前就是这样编译的),也很可能没有做SSL支持。所以还是用命令行工具来编译,先制作一个build.bat文件,待会要用到,内容如下:

[plain] view plain copy
 print?
  1. @REM @echo off  
  2. @IF [%1]==[debug] (  
  3. @echo 正在使用debug模式编译libcurl~~~  
  4. @nmake /f Makefile.vc WITH_DEVEL=../../deps mode=static VC=12 ENABLE_IDN=no RTLIBCFG=dll DEBUG=yes MACHINE=x86  
  5. ) ELSE (  
  6. @echo 正在使用release模式编译libcurl~~~  
  7. @nmake /f Makefile.vc WITH_DEVEL=../../deps mode=static VC=12 ENABLE_IDN=no RTLIBCFG=dll DEBUG=no MACHINE=x86  
  8. )  
  9. @REM @echo on  

        本人下载的是7.48.0版本,接着在开始菜单中打开Visual Studio 2013 > Visual Studio Tools > VS2013开发人员命令提示,cd到curl-7.48.0的winbuild目录下:输入build.bat 按下回车键开始编译,等待编译完成!如果是debug版本,则再输入空格 debug,回车!开始编译。。。最后,贴上一段使用libcurl进行https访问的代码,希望对大家有所帮助。

[cpp] view plain copy
 print?
  1. int https_get(const std::string & strUrl, std::string & strResponse, const char * pCaPath)  
  2. {  
  3.     CURLcode res;  
  4.     CURL* curl = curl_easy_init();  
  5.     if(NULL == curl)  
  6.     {  
  7.         return CURLE_FAILED_INIT;  
  8.     }  
  9.   
  10.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
  11.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  12.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
  13.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  14.     if(NULL == pCaPath)  
  15.     {  
  16.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);//设定为不验证证书和HOST  
  17.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
  18.     }  
  19.     else  
  20.     {  
  21.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);  
  22.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  
  23.     }  
  24.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
  25.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  26.     res = curl_easy_perform(curl);  
  27.     curl_easy_cleanup(curl);  
  28.   
  29.     return res;  
  30. }  

PS:使用libcurl作为http客户端,可以参考一下:http://blog.csdn.net/hellokandy/article/details/53911448

阅读(1940) | 评论(0) | 转发(0) |
0

上一篇:Lua脚本学习3

下一篇:没有了

给主人留下些什么吧!~~