libcurl在嵌入式设备C 的使用

2024-02-22 02:48

本文主要是介绍libcurl在嵌入式设备C 的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

libcurl在嵌入式设备C 的使用_bingqingsuimeng的专栏-CSDN博客

linux:

./configure --prefix=/root/work/code/curl-7.61.1/curl_linux  --disable-shared --enable-static --without-libidn --without-ssl --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6

arm:

./configure --host=arm-hisiv300-linux --prefix=/root/work/code/curl-7.61.1/curl_arm CC=arm-hisiv300-linux-gcc --disable-shared --enable-static --without-libidn --without-ssl --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6

增加 https协议 

./configure --host=arm-hisiv300-linux --prefix=/root/work/code/curl-7.61.1/curl_arm2 CC=arm-hisiv300-linux-gcc --disable-shared --enable-static --without-libidn -with-ssl=/root/work/code/BaseCore/src/rtmpdump/rtmpobj

 

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

#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__#include <string>class CHttpClient
{
public:CHttpClient(void);~CHttpClient(void);public:/*** @brief HTTP POST请求* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com* @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…* @param strResponse 输出参数,返回的内容* @return 返回是否Post成功*/int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);/*** @brief HTTP GET请求* @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com* @param strResponse 输出参数,返回的内容* @return 返回是否Post成功*/int Get(const std::string & strUrl, std::string & strResponse);/*** @brief HTTPS POST请求,无证书版本* @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com* @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…* @param strResponse 输出参数,返回的内容* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.* @return 返回是否Post成功*/int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);/*** @brief HTTPS GET请求,无证书版本* @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com* @param strResponse 输出参数,返回的内容* @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.* @return 返回是否Post成功*/int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);public:void SetDebug(bool bDebug);private:bool m_bDebug;
};#endif
#include "httpclient.h"
#include "curl/curl.h"
#include <string>CHttpClient::CHttpClient(void) : 
m_bDebug(false)
{}CHttpClient::~CHttpClient(void)
{}static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{if(itype == CURLINFO_TEXT){//printf("[TEXT]%s\n", pData);}else if(itype == CURLINFO_HEADER_IN){printf("[HEADER_IN]%s\n", pData);}else if(itype == CURLINFO_HEADER_OUT){printf("[HEADER_OUT]%s\n", pData);}else if(itype == CURLINFO_DATA_IN){printf("[DATA_IN]%s\n", pData);}else if(itype == CURLINFO_DATA_OUT){printf("[DATA_OUT]%s\n", pData);}return 0;
}static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);if( NULL == str || NULL == buffer ){return -1;}char* pData = (char*)buffer;str->append(pData, size * nmemb);return nmemb;
}int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
{CURLcode res;CURL* curl = curl_easy_init();if(NULL == curl){return CURLE_FAILED_INIT;}if(m_bDebug){curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);}curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());curl_easy_setopt(curl, CURLOPT_POST, 1);curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);curl_easy_cleanup(curl);return res;
}int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
{CURLcode res;CURL* curl = curl_easy_init();if(NULL == curl){return CURLE_FAILED_INIT;}if(m_bDebug){curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);}curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);/*** 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。*/curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);curl_easy_cleanup(curl);return res;
}int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
{CURLcode res;CURL* curl = curl_easy_init();if(NULL == curl){return CURLE_FAILED_INIT;}if(m_bDebug){curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);}curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());curl_easy_setopt(curl, CURLOPT_POST, 1);curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);if(NULL == pCaPath){curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);}else{//缺省情况就是PEM,所以无需设置,另外支持DER//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);}curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);curl_easy_cleanup(curl);return res;
}int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
{CURLcode res;CURL* curl = curl_easy_init();if(NULL == curl){return CURLE_FAILED_INIT;}if(m_bDebug){curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);}curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);if(NULL == pCaPath){curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);}else{curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);}curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);res = curl_easy_perform(curl);curl_easy_cleanup(curl);return res;
}///void CHttpClient::SetDebug(bool bDebug)
{m_bDebug = bDebug;
}

这篇关于libcurl在嵌入式设备C 的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/733911

相关文章

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi