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

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1