C++使用libcurl做HttpClient

2023-05-16

C++使用libcurl做HttpClient

分类: 基础技术分享 2012-06-14 19:25  1469人阅读  评论(3)  收藏  举报

    当使用C++做HTTP客户端时,目前通用的做法就是使用libcurl。其官方网站的地址是http://curl.haxx.se/,该网站主要提供了Curl和libcurl。Curl是命令行工具,用于完成FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP的命令的请求及接收回馈。libcurl提供给开发者,用于使用C++跨平台的开发各种网络协议的请求及响应。里面的文档非常齐全,不过都是英文的。

    本文提供最简单的demo使用libcurl开发HttpClient。主要包括同步的HTTP GET、HTTP POST、HTTPS GET、HTTPS POST。

    下载libcurl包,如果使用Linux平台,建议下载源文件编译;如果使用Windows平台,建议下载Win32 - MSVC,下载地址是:http://curl.haxx.se/download.html

[cpp]  view plain copy
  1. #ifndef __HTTP_CURL_H__  
  2. #define __HTTP_CURL_H__  
  3.   
  4. #include <string>  
  5.   
  6. class CHttpClient  
  7. {  
  8. public:  
  9.     CHttpClient(void);  
  10.     ~CHttpClient(void);  
  11.   
  12. public:  
  13.     /** 
  14.     * @brief HTTP POST请求 
  15.     * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com 
  16.     * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&… 
  17.     * @param strResponse 输出参数,返回的内容 
  18.     * @return 返回是否Post成功 
  19.     */  
  20.     int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);  
  21.   
  22.     /** 
  23.     * @brief HTTP GET请求 
  24.     * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com 
  25.     * @param strResponse 输出参数,返回的内容 
  26.     * @return 返回是否Post成功 
  27.     */  
  28.     int Get(const std::string & strUrl, std::string & strResponse);  
  29.   
  30.     /** 
  31.     * @brief HTTPS POST请求,无证书版本 
  32.     * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com 
  33.     * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&… 
  34.     * @param strResponse 输出参数,返回的内容 
  35.     * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性. 
  36.     * @return 返回是否Post成功 
  37.     */  
  38.     int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);  
  39.   
  40.     /** 
  41.     * @brief HTTPS GET请求,无证书版本 
  42.     * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com 
  43.     * @param strResponse 输出参数,返回的内容 
  44.     * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性. 
  45.     * @return 返回是否Post成功 
  46.     */  
  47.     int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);  
  48.   
  49. public:  
  50.     void SetDebug(bool bDebug);  
  51.   
  52. private:  
  53.     bool m_bDebug;  
  54. };  
  55.   
  56. #endif  

[cpp]  view plain copy
  1. #include "httpclient.h"  
  2. #include "curl/curl.h"  
  3. #include <string>  
  4.   
  5. CHttpClient::CHttpClient(void) :   
  6. m_bDebug(false)  
  7. {  
  8.   
  9. }  
  10.   
  11. CHttpClient::~CHttpClient(void)  
  12. {  
  13.   
  14. }  
  15.   
  16. static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)  
  17. {  
  18.     if(itype == CURLINFO_TEXT)  
  19.     {  
  20.         //printf("[TEXT]%s\n", pData);  
  21.     }  
  22.     else if(itype == CURLINFO_HEADER_IN)  
  23.     {  
  24.         printf("[HEADER_IN]%s\n", pData);  
  25.     }  
  26.     else if(itype == CURLINFO_HEADER_OUT)  
  27.     {  
  28.         printf("[HEADER_OUT]%s\n", pData);  
  29.     }  
  30.     else if(itype == CURLINFO_DATA_IN)  
  31.     {  
  32.         printf("[DATA_IN]%s\n", pData);  
  33.     }  
  34.     else if(itype == CURLINFO_DATA_OUT)  
  35.     {  
  36.         printf("[DATA_OUT]%s\n", pData);  
  37.     }  
  38.     return 0;  
  39. }  
  40.   
  41. static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)  
  42. {  
  43.     std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);  
  44.     if( NULL == str || NULL == buffer )  
  45.     {  
  46.         return -1;  
  47.     }  
  48.   
  49.     char* pData = (char*)buffer;  
  50.     str->append(pData, size * nmemb);  
  51.     return nmemb;  
  52. }  
  53.   
  54. int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)  
  55. {  
  56.     CURLcode res;  
  57.     CURL* curl = curl_easy_init();  
  58.     if(NULL == curl)  
  59.     {  
  60.         return CURLE_FAILED_INIT;  
  61.     }  
  62.     if(m_bDebug)  
  63.     {  
  64.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
  65.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
  66.     }  
  67.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
  68.     curl_easy_setopt(curl, CURLOPT_POST, 1);  
  69.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  
  70.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  71.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
  72.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
  73.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  74.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
  75.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  76.     res = curl_easy_perform(curl);  
  77.     curl_easy_cleanup(curl);  
  78.     return res;  
  79. }  
  80.   
  81. int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)  
  82. {  
  83.     CURLcode res;  
  84.     CURL* curl = curl_easy_init();  
  85.     if(NULL == curl)  
  86.     {  
  87.         return CURLE_FAILED_INIT;  
  88.     }  
  89.     if(m_bDebug)  
  90.     {  
  91.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
  92.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
  93.     }  
  94. <pre name="code" class="cpp">   curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
  95.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  96.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
  97.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
  98.     /** 
  99.     * 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。 
  100.     * 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。 
  101.     */  
  102.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  103.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
  104.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  105.     res = curl_easy_perform(curl);  
  106.     curl_easy_cleanup(curl);  
  107.     return res;  
  108. }  
  109.   
  110. int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)  
  111. {  
  112.     CURLcode res;  
  113.     CURL* curl = curl_easy_init();  
  114.     if(NULL == curl)  
  115.     {  
  116.         return CURLE_FAILED_INIT;  
  117.     }  
  118.     if(m_bDebug)  
  119.     {  
  120.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
  121.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
  122.     }  
  123.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
  124.     curl_easy_setopt(curl, CURLOPT_POST, 1);  
  125.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());  
  126.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  127.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
  128.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
  129.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  130.     if(NULL == pCaPath)  
  131.     {  
  132.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);  
  133.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
  134.     }  
  135.     else  
  136.     {  
  137.         //缺省情况就是PEM,所以无需设置,另外支持DER  
  138.         //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");  
  139.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);  
  140.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  
  141.     }  
  142.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
  143.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  144.     res = curl_easy_perform(curl);  
  145.     curl_easy_cleanup(curl);  
  146.     return res;  
  147. }  
  148.   
  149. int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)  
  150. {  
  151.     CURLcode res;  
  152.     CURL* curl = curl_easy_init();  
  153.     if(NULL == curl)  
  154.     {  
  155.         return CURLE_FAILED_INIT;  
  156.     }  
  157.     if(m_bDebug)  
  158.     {  
  159.         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);  
  160.         curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);  
  161.     }  
  162.     curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());  
  163.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
  164.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);  
  165.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);  
  166.     curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  
  167.     if(NULL == pCaPath)  
  168.     {  
  169.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);  
  170.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);  
  171.     }  
  172.     else  
  173.     {  
  174.         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);  
  175.         curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);  
  176.     }  
  177.     curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);  
  178.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);  
  179.     res = curl_easy_perform(curl);  
  180.     curl_easy_cleanup(curl);  
  181.     return res;  
  182. }  
  183.   
  184. ///  
  185.   
  186. void CHttpClient::SetDebug(bool bDebug)  
  187. {  
  188.     m_bDebug = bDebug;  
  189. }  
  190. </pre><p></p>  
  191. <pre></pre>  
  192. <br>  
  193. <br>  
  194. <p></p>  
  195. <br>  
分享到: 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++使用libcurl做HttpClient 的相关文章

随机推荐

  • Nuttx romfs与启动脚本rcS

    ARM系统上电后 xff0c 系统将flash地址映射到零地址处 xff0c 处理器从零地址处开始运行第一条指令 而在零地址处 xff0c 一般是系统复位中断向量 xff0c 此处存放的是一条跳转指指令 xff0c 通过该条换指令 xff0
  • 在终端/命令行下打开文件浏览器窗口--Win cmd &Ubuntu terminal

    在命令行下想要可视化查看文件 xff0c 可以使用命令直接打开图形化窗口 1 Windows windows上可以使用explorer exe打开资源管理器 xff1a explorer exe span class token keywo
  • 127.0.0.0与0.0.0.0的区别

    1 IP地址分类 ref https tools ietf org html rfc1700 page 3 IP地址表示 IP地址由两个部分组成 xff0c net id和host id xff0c 即网络号和主机号 net id 表示ip
  • 【python】代码换行的几种方法

    代码太长怎么办 xff0c 反斜杠 引号 34 34 34 39 来帮忙 xff01 在写list或者较长的字符串时候 xff0c 或者多个循环造成IDE不够用时 xff0c 就需要代码换行了 主要的代码换行有通用的反斜杠 和针对字符串起作
  • 自己动手写C++迭代器

    综述 关于STL iterator和 iterator adapter 的部分我已在先前的博客 stl源码剖析笔记之iterator 中有所提及 xff0c 下面我们可以试着自己动手写一个简单的迭代器工具 step iterator xff
  • 【STM32F0】Keil 查看局部变量显示<not in scope>

    现象 xff1a 在进行STM32F0开发的时候出现了 调试代码 xff0c 添加变量Watch时 xff0c 显示not in scope 处理方式 xff1a 因为代码开了优化的处理 xff0c 把优化改到Level0 就可以解决问题
  • error: undefined reference to '__dso_handle'解决方案

    error undefined reference to 39 dso handle 39 解决方案 home NDK android ndk r9 sources cxx stl gnu libstdc 43 43 4 7 libs ar
  • ARM里的大端格式和小端格式分别是什么意思?

    当前的存储器 xff0c 多以byte为访问的最小单元 xff0c 当一个逻辑上的地址必须分割为物理上的若干单元时就存在了先放谁后放谁的问题 于是端 endian 的问题应运而生了 对于不同的存储方法 就有大端 big endian 和小端
  • STM32串口通信(基于缓冲区)编程及遇到的问题总结

    在写串口通信前阅读了STM32中文参考手册 xff0c 然后满心澎湃地写代码 在这个过程中遇一些让人郁闷的事情 xff0c 目前这些问题目前已经解决了 xff08 嘿嘿嘿 xff09 xff0c 特此来总结一番 串口的使用步骤大概如下 xf
  • 主板串口FIFO大小设置

    FIFO大小和芯片 驱动有关系 xff0c 可以在设备管理器里面调整大小 1 进入设备管理器 xff0c 右键选择串口属性 2 在端口设置里面选择高级 3 可以单独设定每一个串口的接收和发送区大小
  • mac终端 python scrapy爬虫 zsh: no matches found

    在学习Python爬虫时 xff0c 进行到scrapy板块 xff0c 执行genspider命令 输入scrapy genspider tongcheng https bj 58 com sou key 61 E5 89 8D E7 A
  • JSESSIONID的简单说明

    1 第一次访问服务器的时候 xff0c 会在响应头里面看到Set Cookie信息 只有在首次访问服务器的时候才会在响应头中出现该信息 上面的图JSESSIONID 61 ghco9xdnaco31gmafukxchph Path 61 a
  • 史上最全的常用开发工具类收集(持续更新中)

    外链图片转存失败 源站可能有防盗链机制 建议将图片保存下来直接上传 img BtY85pbk 1577412535564 https img shields io badge QQ群 523167548 20 ff69b4 svg API
  • MFC学习之vc通过HTTP请求:Get或Post方式获取JSON信息(亲测可用)

    前段时间公司项目需要跟上一级平台对接一些采集回来的数据 xff0c 通过HTTP xff0c post方法上传JSON信息到指定的接口地址 本来呢 xff1f 我在入职时是面试的售后岗 xff0c 一家小公司 xff0c 当时公司软件方面一
  • mac安装虚拟机VMware fusion12 和ubantu系统

    一 基本安装 下载虚拟机VMware fusion12 我选择了不更新新版本并且允许访问辅助功能 选择 新建 接下来选择 从光盘或者映像安装 从下载目录把ubantu系统拖拽过去 点击安装完成 xff0c 将ubantu系统保存在虚拟机上即
  • VSCode常用命令---记录自己的常用命令

    一 nvm相关命令 node版本管理 查看已安装的版本 nvm list 使用版本 nvm use 版本号 安装版本 nvm install 版本号 卸载版本 nvm uninstall 版本号 常用命令 全局包安装 多用于gitee下载后
  • Ubuntu18.04 Realsense D435i驱动安装与配置

    InterRealSenseD435i SDK安装 一 命令行的安装方式安装 1 注册服务器的公钥 xff1a 打开终端输入 sudo apt key adv keyserver keys gnupg net recv key C8B3A5
  • Qt上位机:与STM32串口通信,数据收发,按钮控制LED

    Qt学习了几周 xff0c 做一个串口助手巩固一下最近学习的内容 遇到的问题1 xff1a write函数只能发送一次数据 xff0c 想要继续发送必须重新关闭打开串口 xff0c 每次只能发送一次数据 解决办法 xff1a 在网上找不到类
  • 考研数据结构2 | 使用 C++ 实现顺序栈 | 栈的基本应用之计算后缀表达式

    文章目录 1 顺序栈 简介2 顺序栈 代码实现3 栈的应用之计算后缀表达式3 1 表达式介绍3 2 计算后缀表达式的实现3 3 完整代码3 4 LeetCode 提交代码 1 顺序栈 简介 在上一次的学习中 xff0c 使用指针实现了链栈
  • C++使用libcurl做HttpClient

    C 43 43 使用libcurl做HttpClient 分类 xff1a 基础技术分享 2012 06 14 19 25 1469人阅读 评论 3 收藏 举报 当使用C 43 43 做HTTP客户端时 xff0c 目前通用的做法就是使用l