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使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

Java之并行流(Parallel Stream)使用详解

《Java之并行流(ParallelStream)使用详解》Java并行流(ParallelStream)通过多线程并行处理集合数据,利用Fork/Join框架加速计算,适用于大规模数据集和计算密集... 目录Java并行流(Parallel Stream)1. 核心概念与原理2. 创建并行流的方式3. 适

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件