当前位置: 首页 > news >正文

curl封装

一。由于工作的原因,需要对curl做一些封装,附加上我们的证书,提供给第三个C++和jAVA使用。

二。头文件封闭四个函数,get,post,download,upload

#ifndef CURLHTTP_H
#define CURLHTTP_H#include <iostream>
#include <string>
#include <curl/curl.h>class CurlHttp {
public:
CurlHttp();
~CurlHttp();CURLcode get(const std::string& url, std::string& response);
CURLcode post(const std::string& url, const std::string& data, std::string& response);
CURLcode download(const std::string& url, const std::string& savePath);
CURLcode upload(const std::string& url, const std::string& filePath, std::string& response);
private:
CURL* curl;static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response);
void setSSLSettings();
};#endif // CURLHTTP_H

三。实现Cpp,返回一个CURLcode方便出错时追踪错误

#include "CurlHttp.h"CurlHttp::CurlHttp() {curl = curl_easy_init();if (!curl) {std::cerr << "Failed to initialize cURL" << std::endl;}
}CurlHttp::~CurlHttp() {if (curl) {curl_easy_cleanup(curl);}
}CURLcode CurlHttp::get(const std::string& url, std::string& response) {CURLcode res = CURLE_FAILED_INIT;if (curl) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);setSSLSettings();res = curl_easy_perform(curl);if (res != CURLE_OK) {std::cerr << "cURL GET request failed: " << curl_easy_strerror(res) << std::endl;}}return res;
}CURLcode CurlHttp::post(const std::string& url, const std::string& data, std::string& response) {CURLcode res = CURLE_FAILED_INIT;if (curl) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);setSSLSettings();// 设置请求头为JSON类型struct curl_slist* headers = nullptr;headers = curl_slist_append(headers, "Content-Type: application/json");curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);res = curl_easy_perform(curl);if (res != CURLE_OK) {std::cerr << "cURL POST request failed: " << curl_easy_strerror(res) << std::endl;}}return res;
}CURLcode CurlHttp::download(const std::string& url, const std::string& savePath) {CURLcode res = CURLE_FAILED_INIT;if (curl) {FILE* file = fopen(savePath.c_str(), "wb");if (file) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);setSSLSettings();res = curl_easy_perform(curl);if (res != CURLE_OK) {std::cerr << "cURL download failed: " << curl_easy_strerror(res) << std::endl;}fclose(file);} else {std::cerr << "Failed to open file for writing: " << savePath << std::endl;res = CURLE_FAILED_INIT;}}return res;
}CURLcode CurlHttp::upload(const std::string& url, const std::string& filePath, std::string& response) {CURLcode res = CURLE_FAILED_INIT;if (curl) {FILE* file = fopen(filePath.c_str(), "rb");if (file) {curl_easy_setopt(curl, CURLOPT_URL, url.c_str());curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);curl_easy_setopt(curl, CURLOPT_READDATA, file);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);setSSLSettings();res = curl_easy_perform(curl);if (res != CURLE_OK) {std::cerr << "cURL upload failed: " << curl_easy_strerror(res) << std::endl;}fclose(file);} else {std::cerr << "Failed to open file for reading: " << filePath << std::endl;res = CURLE_FAILED_INIT;}}return res;
}size_t CurlHttp::WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) {size_t total_size = size * nmemb;//response->append((char*)contents, total_size);response->append(static_cast<char*>(contents), totalSize);return total_size;
}void CurlHttp::setSSLSettings() {// 设置证书路径curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");// 设置私钥路径curl_easy_setopt(curl, CURLOPT_SSLKEY, "/path/to/private.key");// 设置私钥密码(如果有的话)curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "password");
}

四。测试函数:

#include <iostream>
#include "CurlHttp.h"int main() {CurlHttp curlHttp;// 发起 GET 请求std::string url = "https://api.example.com/data";std::string response;CURLcode res = curlHttp.get(url, response);if (res == CURLE_OK) {std::cout << "GET request successful. Response: " << response << std::endl;} else {std::cerr << "GET request failed. Error: " << curl_easy_strerror(res) << std::endl;}// 发起 POST 请求url = "https://api.example.com/post";std::string data = "key1=value1&key2=value2";response.clear();res = curlHttp.post(url, data, response);if (res == CURLE_OK) {std::cout << "POST request successful. Response: " << response << std::endl;} else {std::cerr << "POST request failed. Error: " << curl_easy_strerror(res) << std::endl;}// 下载文件url = "https://example.com/file.jpg";std::string savePath = "/path/to/save/file.jpg";res = curlHttp.download(url, savePath);if (res == CURLE_OK) {std::cout << "File downloaded successfully and saved at: " << savePath << std::endl;} else {std::cerr << "File download failed. Error: " << curl_easy_strerror(res) << std::endl;}// 上传文件url = "https://api.example.com/upload";std::string filePath = "/path/to/upload/file.txt";response.clear();res = curlHttp.upload(url, filePath, response);if (res == CURLE_OK) {std::cout << "File uploaded successfully. Response: " << response << std::endl;} else {std::cerr << "File upload failed. Error: " << curl_easy_strerror(res) << std::endl;}return 0;
}

六。创建一个aidl文件

package com.example.yourpackage; // 替换为您的包名interface ICurlHttpService {int get(in String url, out String response);int post(in String url, in String data, out String response);int download(in String url, in String savePath);int upload(in String url, in String filePath, out String response);
}

相关文章:

curl封装

一。由于工作的原因&#xff0c;需要对curl做一些封装&#xff0c;附加上我们的证书&#xff0c;提供给第三个C和jAVA使用。 二。头文件封闭四个函数&#xff0c;get&#xff0c;post&#xff0c;download&#xff0c;upload #ifndef CURLHTTP_H #define CURLHTTP_H#include …...

C语言数据类型和变量

C语言数据类型和变量 数据类型分类内置类型【C语言本身就具有的类型】自定义类型【自己来创建类型】取值范围 变量变量的创建变量创建的语法形式变量的分类全局变量局部变量 栈区、堆区、静态区 算术操作符赋值操作符连续赋值复合赋值符 单目操作符&#xff1a;、--、、-强制类…...

分布式训练 最小化部署docker swarm + docker-compose落地方案

目录 背景&#xff1a; 前提条件&#xff1a; 一、docker环境初始化配置 1. 安装nvidia-docker2 2. 安装docker-compose工具 3. 获取GPU UUID 4. 修改docker runtime为nvidia&#xff0c;指定机器的UUID 二、docker-swarm 环境安装 1. 初始化swarm管理节点 2. 加入工…...

QT学习笔记-开发环境编译Qt MySql数据库驱动与交叉编译Qt MySql数据库驱动

QT学习笔记-开发环境编译Qt MySql数据库驱动与交叉编译Qt MySql数据库驱动 0、背景1、基本环境2、开发环境编译Qt MySql数据库驱动2.1 依赖说明2.2 MySQL驱动编译过程 3、交叉编译Qt MySql数据库驱动3.1 依赖说明3.3.1 如何在交叉编译服务器上找到mysql.h及相关头文件3.3.2 如果…...

QT使用QXlsx实现数据验证与Excel公式操作 QT基础入门【Excel的操作】

准备环境:QT中使用QtXlsx库的三种方法 1、公式操作写单行公式 //右值初始化Format rAlign;rAlign.setHorizontalAlignment(Format::AlignRight);//左值初始化Format lAlign;lAlign.setHorizontalAlignment(Format::AlignLeft);xlsx.write("B3", 40, lAlign);xlsx.wr…...

renrenfast Vue2 打包发布

1、修改 static/config/index-prod.js 文件 // api接口请求地址 window.SITE_CONFIG[baseUrl] http://192.168.1.86:8080/renren-fast; /*** 生产环境*/ ;(function () {window.SITE_CONFIG {};// api接口请求地址window.SITE_CONFIG[baseUrl] http://192.16…...

NoSQL数据库介绍+Redis部署

目录 一、NoSQL概述 1、数据的高并发读写 2、海量数据的高效率存储和访问 3、数据库的高扩展和高可用 二、NoSQL的类别 1、键值存储数据库 2、列存储数据库 3、文档型数据库 4、图形化数据库 三、分布式数据库中的CAP原理 1、传统的ACID 1&#xff09;、A--原子性 …...

【mindspore学习】环境配置

本次实验搭配的环境是 CUDA 11.6 CUDNN v8.9.4 TensorRT-8.4.1.5 mindspore 2.1.0。 1、配置 Nvidia 显卡驱动 如果原来的主机已经安装了 nvidia 驱动&#xff0c;为避免版本的冲突&#xff0c;建议先清除掉旧的 nvidia驱动 sudo apt-get --purge remove nvidia* sudo apt…...

基于shell脚本对aliyun npm仓库(https://packages.aliyun.com)登录认证

文章目录 基于shell脚本对阿里云npm仓库&#xff08;https://packages.aliyun.com&#xff09;登录认证食用人群食用方式 基于shell脚本对阿里云npm仓库&#xff08;https://packages.aliyun.com&#xff09;登录认证 食用人群 由于一些安全的原因&#xff0c;某些企业可能会…...

K8s Pod 安全认知:从openshift SCC 到 PSP 弃用以及现在的 PSA

写在前面 简单整理,博文内容涉及: PSP 的由来PSA 的发展PSA 使用认知不涉及使用,用于了解 Pod 安全 API 资源理解不足小伙伴帮忙指正对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是…...

提高企业会计效率,选择Manager for Mac(企业会计软件)

作为一家企业&#xff0c;良好的财务管理是保持业务运转的关键。而选择一款适合自己企业的会计软件&#xff0c;能够帮助提高会计效率、减少错误和节约时间。在众多的选择中&#xff0c;Manager for Mac(企业会计软件)是一款值得考虑的优秀软件。 首先&#xff0c;Manager for…...

软考:中级软件设计师:信息系统的安全属性,对称加密和非对称加密,信息摘要,数字签名技术,数字信封与PGP

软考&#xff1a;中级软件设计师:信息系统的安全属性 提示&#xff1a;系列被面试官问的问题&#xff0c;我自己当时不会&#xff0c;所以下来自己复盘一下&#xff0c;认真学习和总结&#xff0c;以应对未来更多的可能性 关于互联网大厂的笔试面试&#xff0c;都是需要细心准…...

Vue3中reactive响应式失效的问题

情景阐述 弹窗内部有一个挑选框&#xff0c;要通过请求接口获取挑选框下面可供选择的数据。 这是一个很简单的情境&#xff0c;我立刻有了自己的思路。如果实现搜索&#xff0c;数据较少可以直接用elementplus自带的filter。如果数据较多&#xff0c;就需要传val&#xff0c;…...

lamp

LAMP 环境 指的是在 Linux 操作系统中分别安装 Apache 网页服务器、MySQL 数据库服务器和 PHP 开发服务器&#xff0c;以及一些对应的扩展软件。AMP也支持win操作系统 &#xff08;sccm 域升级版&#xff09; LAMP架构是目前成熟的企业网站应用模式之一&#xff0c;指的是协同…...

LeetCode 周赛上分之旅 #42 当 LeetCode 考树上倍增,出题的趋势在变化吗

⭐️ 本文已收录到 AndroidFamily&#xff0c;技术和职场问题&#xff0c;请关注公众号 [彭旭锐] 和 BaguTree Pro 知识星球提问。 学习数据结构与算法的关键在于掌握问题背后的算法思维框架&#xff0c;你的思考越抽象&#xff0c;它能覆盖的问题域就越广&#xff0c;理解难度…...

Qt 自定义菜单 托盘菜单

托盘菜单实现&#xff1a;通过QSystemTrayIconQMenuQAction即可完美实现&#xff01; 实现方式&#xff1a;createActions用于创建菜单、菜单项,translateActions用于设置文本、实现多语化&#xff0c;translateAccount用于设置用户空间配额。 void TrayMenu::createActions(…...

channel并发编程

不要通过共享内存通信&#xff0c;要通过通信共享内存。 channel是golang并发编程中一种重要的数据结构&#xff0c;用于多个goroutine之间进行通信。 我们通常可以把channel想象成一个传送带&#xff0c;将goroutine想象成传送带周边的人&#xff0c;一个传送带的上游放上物品…...

苹果新健康专利:利用 iPhone、Apple Watch 来分析佩戴者的呼吸情况

根据美国商标和专利局&#xff08;USPTO&#xff09;公示的清单&#xff0c;苹果获得了一项健康相关的技术专利&#xff0c;可以利用 iPhone、Apple Watch 来分析佩戴者的呼吸系统。 苹果在专利中概述了一种测量用户呼吸功能的系统&#xff0c;通过 iPhone 上的光学感测单元&am…...

数据分析基础-数据可视化02-不同数据类型的可视化概念及原则

将数据空间映射到颜色空间。 数据空间&#xff1a;连续或分类 数据可以被划分为两个主要的数据空间&#xff1a;连续数据和分类数据。这两种数据空间有不同的特点和适用的分析方法。 连续数据&#xff08;Continuous Data&#xff09;&#xff1a; 连续数据是指可以在某个范…...

QT项目使用Qss的总结

什么是QSS QSS称为Qt Style Sheets也就是Qt样式表&#xff0c;它是Qt提供的一种用来自定义控件外观的机制。QSS大量参考了CSS的内容&#xff0c;只不过QSS的功能比CSS要弱很多&#xff0c;体现在选择器要少&#xff0c;可以使用的QSS属性也要少很多&#xff0c;并且并不是所有…...

C++实现分布式网络通信框架RPC(3)--rpc调用端

目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中&#xff0c;我们已经大致实现了rpc服务端的各项功能代…...

【机器视觉】单目测距——运动结构恢复

ps&#xff1a;图是随便找的&#xff0c;为了凑个封面 前言 在前面对光流法进行进一步改进&#xff0c;希望将2D光流推广至3D场景流时&#xff0c;发现2D转3D过程中存在尺度歧义问题&#xff0c;需要补全摄像头拍摄图像中缺失的深度信息&#xff0c;否则解空间不收敛&#xf…...

基于当前项目通过npm包形式暴露公共组件

1.package.sjon文件配置 其中xh-flowable就是暴露出去的npm包名 2.创建tpyes文件夹&#xff0c;并新增内容 3.创建package文件夹...

微服务商城-商品微服务

数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...

今日科技热点速览

&#x1f525; 今日科技热点速览 &#x1f3ae; 任天堂Switch 2 正式发售 任天堂新一代游戏主机 Switch 2 今日正式上线发售&#xff0c;主打更强图形性能与沉浸式体验&#xff0c;支持多模态交互&#xff0c;受到全球玩家热捧 。 &#x1f916; 人工智能持续突破 DeepSeek-R1&…...

图表类系列各种样式PPT模版分享

图标图表系列PPT模版&#xff0c;柱状图PPT模版&#xff0c;线状图PPT模版&#xff0c;折线图PPT模版&#xff0c;饼状图PPT模版&#xff0c;雷达图PPT模版&#xff0c;树状图PPT模版 图表类系列各种样式PPT模版分享&#xff1a;图表系列PPT模板https://pan.quark.cn/s/20d40aa…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

Go语言多线程问题

打印零与奇偶数&#xff08;leetcode 1116&#xff09; 方法1&#xff1a;使用互斥锁和条件变量 package mainimport ("fmt""sync" )type ZeroEvenOdd struct {n intzeroMutex sync.MutexevenMutex sync.MutexoddMutex sync.Mutexcurrent int…...

【Linux手册】探秘系统世界:从用户交互到硬件底层的全链路工作之旅

目录 前言 操作系统与驱动程序 是什么&#xff0c;为什么 怎么做 system call 用户操作接口 总结 前言 日常生活中&#xff0c;我们在使用电子设备时&#xff0c;我们所输入执行的每一条指令最终大多都会作用到硬件上&#xff0c;比如下载一款软件最终会下载到硬盘上&am…...

电脑桌面太单调,用Python写一个桌面小宠物应用。

下面是一个使用Python创建的简单桌面小宠物应用。这个小宠物会在桌面上游荡&#xff0c;可以响应鼠标点击&#xff0c;并且有简单的动画效果。 import tkinter as tk import random import time from PIL import Image, ImageTk import os import sysclass DesktopPet:def __i…...