由浅入深C系列五:使用libcurl进行基于http get/post模式的C语言交互应用开发

使用libcurl进行基于http get/post模式的C语言交互应用开发

  • 简介
  • 环境准备
  • 在线资源
  • 示例代码
  • 测试调用
  • 运行结果

简介

大多数在linux下的开发者,都会用到curl这个命令行工具。对于进行restful api的测试等,非常方便。其实,这个工具还提供了一个C的开发库,可以很方便的在C语言开发环境下完成基于http的请求和响应交互,高效的开发基于http/smtp等的网络应用程序

/* 2023-08-14 更新宏定义 1. 使用可变参数,支持多项输出; 2. 去除Z中默认加上的双引号; 
*/
#define X_LOG_DEBUG(Z, X...) \printf("[%s %s] [%s.%d] [%s] [DEBUG] " Z "\n", __DATE__, __TIME__, __FILE__, __LINE__, __FUNCTION__, ##X)

环境准备

下载并安装curl的开发包

yum install libcurl-devel.x86_64

在线资源

开发过程中主要参考CURL官方介绍及API参考文档 | link

示例代码

多余的话就不多说了,直接上示例代码,通过代码中的注释来说明开发过程。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../include/xhttp.h"/*
-  这个是一个回调函数,主要用于curl在执行过程中,当有被请求的数据到达时,被调用来向-curl_easy_setopt设置的chunk中写入数据。这个回调函数在curl_easy_perform执行完成前会被调用多次。当执行完成后,从chunk中取出这次交互返回的数据。
*/
static size_t cb_write_data(void *data, size_t size, size_t nmemb, void *clientp)
{size_t realsize = size * nmemb;http_response *mem = (http_response *)clientp;char *ptr = realloc(mem->response, mem->size + realsize + 1);if(ptr == NULL)return 0;  /* out of response_st! */mem->response = ptr;memcpy(&(mem->response[mem->size]), data, realsize);mem->size += realsize;mem->response[mem->size] = 0;return realsize;
}/*这个是向外发布的一个函数,调用的方式示例如下:char* payload = "{\"code\":\"\",\"codeUuid\":\"\",\"loginName\":\"user@domain\",\"loginPwd\":\"xxxxxxxx\"}";http_response *resp = http_post("https://local.domain/admin-api/session/login", NULL, payload);使用完返回数据后,记得释放resp->reesponse,避免内存漏。
*/
http_response* http_post(char* url, char* token, char* payload) 
{http_response chunk = {0};/* 设置curl上下文,对curl实例进行初始化 */curl_global_init(CURL_GLOBAL_ALL);CURL *curl = curl_easy_init();CURLcode res;if(curl) {X_LOG_DEBUG("%s", "libcurl curl_easy_setopt start ...");/* 设置curl各个参数: 请求地址 */curl_easy_setopt(curl, CURLOPT_URL, url);/* 设置curl各个参数: 请求方式为post */curl_easy_setopt(curl, CURLOPT_POST, 1L);/* 设置curl各个参数: http中的请求头部分的内容 */X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_HTTPHEADER start ...");struct curl_slist *list = {0};list = curl_slist_append(NULL, "Content-Type: application/json;charset=utf8");list = curl_slist_append(list, "routeurl: /project/project-list");/* 设置curl各个参数: 可选部分,如果请求中要求token,可以设置上 */if (token != NULL){char* x_access_token_str = (char*)malloc( MAX_UTMP_TOKEN_SIZE );sprintf(x_access_token_str, "x-access-token: %s", token);X_LOG_DEBUG("%s", x_access_token_str);list = curl_slist_append(list, x_access_token_str);}curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_USERAGENT start ...");/* some servers do not like requests that are made without a user-agent field, so we provide one */curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0");/* 设置curl各个参数: http的请求体部分,主要是请求中携带的数据 *//* POST data */X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_POSTFIELDSIZE/CURLOPT_POSTFIELDS start ...");X_LOG_DEBUG("request body data is:%s", payload);X_LOG_DEBUG("request body len is:%d", strlen(payload));/* size of the POST data */curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(payload));/* pass in a pointer to the data - libcurl will not copy */curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);/* 设置curl各个参数: 重要部分,设置了返回值的回调函数和返回值的内存放置区域 *//* RECEIVE DATA */X_LOG_DEBUG("%s", "libcurl curl_easy_setopt CURLOPT_WRITEFUNCTION/CURLOPT_WRITEDATA start ...");/* send all data to this function  */curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb_write_data);/* we pass our 'chunk' struct to the callback function */curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);X_LOG_DEBUG("%s", "libcurl curl_easy_setopt successfully complete ...");/* 执行curl请求,并获取返回值:CURLE_OK,表示执行成功 */X_LOG_DEBUG("%s", "libcurl curl_easy_perform start ...");res = curl_easy_perform(curl);X_LOG_DEBUG("%s", "libcurl curl_easy_perform successfully complete ...");if(res != CURLE_OK) {fprintf(stderr, "curl_easy_perform() is failed: %s", curl_easy_strerror(res));curl_slist_free_all(list); /* free the list again */curl_easy_cleanup(curl);} else {/* 处理curl返回的数据 */X_LOG_DEBUG("chunk size %d", chunk.size);http_response response = {0};response.response = (char*)malloc(1048576*5);memset(response.response, 0, chunk.size+1);memcpy(response.response, chunk.response, chunk.size+1);response.size = chunk.size;/* remember to free the buffer */free(chunk.response);curl_slist_free_all(list); /* free the list again */ curl_global_cleanup();return &response;}} 
}

引用的头文件如下:

#ifndef __X_HTTP_H__
#define __X_HTTP_H__#include <stdlib.h>
#include <curl/curl.h>
/* 更新宏定义 1. 使用可变参数,支持多项输出; 2. 去除Z中默认加上的双引号; */
#define X_LOG_DEBUG(Z, X...) \printf("[%s %s] [%s.%d] [%s] [DEBUG] " Z "\n", __DATE__, __TIME__, __FILE__, __LINE__, __FUNCTION__, ##X)#define MAX_UTMP_TOKEN_SIZE 8192typedef struct http_response_s {char *response;size_t size;
} http_response;typedef struct http_request_s {char *request;size_t size;
} http_request;http_response* http_post(char* url, char* token, char* payload);#endif

测试调用

int main(int argc, char** argv) {char* payload = "{\"code\":\"\",\"codeUuid\":\"\",\"loginName\":\"user@domain\",\"loginPwd\":\"xxxxxxxx\"}";http_response *resp = http_post("https://local.domain/admin-api/session/login", NULL, payload);printf("http_response [%d] is: %s\n", resp->size, resp->response);char *tokenVal = strstr(resp->response, "xaccessToken");int end = strlen(tokenVal);*(tokenVal + end-2) = 0;char** token_arr;__strtok_r(tokenVal, ":", token_arr);char* replacementKey = strtrim(token_arr[0], '\"');exit(0)
}

运行结果

[Aug 11 2023 10:20:02] [src/xhttp.c.37] [http_post] [DEBUG] "libcurl curl_easy_setopt start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.42] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_HTTPHEADER start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.55] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_USERAGENT start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.61] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_POSTFIELDSIZE/CURLOPT_POSTFIELDS start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.62] [http_post] [DEBUG] "request body data is:{"code":"","codeUuid":"","loginName":"user@domain","loginPwd":"xxxxxxxx"}"
[Aug 11 2023 10:20:02] [src/xhttp.c.63] [http_post] [DEBUG] "request body len is:75"
[Aug 11 2023 10:20:02] [src/xhttp.c.70] [http_post] [DEBUG] "libcurl curl_easy_setopt CURLOPT_WRITEFUNCTION/CURLOPT_WRITEDATA start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.76] [http_post] [DEBUG] "libcurl curl_easy_setopt successfully complete ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.79] [http_post] [DEBUG] "libcurl curl_easy_perform start ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.81] [http_post] [DEBUG] "libcurl curl_easy_perform successfully complete ..."
[Aug 11 2023 10:20:02] [src/xhttp.c.90] [http_post] [DEBUG] "chunk size 6693"

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/37802.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python中单引号、双引号和三引号的区别

① 单引号和双引号主要用来表示字符串 # 单引号 astr = Python print(type(astr)) # <class str># 双引号"" bstr = "Python" print(type(bstr)) # <class str> str1 = I\m a big fan of Python. print(str1) # Im a big fan of Python.s…

[HDLBits] Exams/m2014 q4d

Implement the following circuit: module top_module (input clk,input in, output out);always(posedge clk) beginout<out^in;end endmodule直接写out^in就行

Vue 使用 vite 创建项目

vite 是新一代前端构建工具&#xff0c;和 webpack 类似。 vite 的启动速度更快。在开发环境中&#xff0c;不需要打包就可以直接运行。 vite 的更新速度更快。当修改内容后&#xff0c;不需要刷新浏览器&#xff0c;页面就会实时更新。 vite 完全是按需编译。它只会编译需要…

【考研数学】高等数学第三模块——积分学 | Part II 定积分(反常积分及定积分应用)

文章目录 前言三、广义积分3.1 敛散性概念&#xff08;一&#xff09;积分区间为无限的广义积分&#xff08;二&#xff09;积分区间有限但存在无穷间断点 3.2 敛散性判别法 四、定积分应用写在最后 前言 承接前文&#xff0c;梳理完定积分的定义及性质后&#xff0c;我们进入…

企业网盘 vs 传统存储设备:为何云存储成为首选?

企业网盘的出现为企业提供了新的存储方式&#xff0c;相较于传统的存储设备&#xff0c;为何越来越多的企业选择了云存储呢&#xff1f; 一、存储成本 在企业数据存储方面&#xff0c;成本是企业重要的考量因素。企业网盘是基于云存储技术的存储工具&#xff0c;因此它比传统的…

基于DBN的伪测量配电网状态估计,DBN的详细原理

目录 背影 DBN神经网络的原理 DBN神经网络的定义 受限玻尔兹曼机(RBM) DBN的伪测量配电网状态估计 基本结构 主要参数 数据 MATALB代码 结果图 展望 背影 DBN是一种深度学习神经网络,拥有提取特征,非监督学习的能力,是一种非常好的分类算法,本文将DBN算法伪测量配电网…

Netty:ByteBuf的资源释放方法

说明 io.netty.buffer.ByteBuf实现了io.netty.util.ReferenceCounted接口&#xff0c;需要显式释放。当ByteBuf被实例化后&#xff0c;它的引用计数是1。 调用ByteBuf对象的release方法释放&#xff1a; ByteBuf的release()方法使引用计数减少1。只有当执行以后引用计数减少…

OpenZFS 2.2 发布 RC3,支持 Linux 6.4

导读之前的 OpenZFS 2.2 候选版本已致力于实现与 Linux 6.4 内核的兼容性&#xff0c;而在 2.2-rc3 中&#xff0c;Linux 6.4 支持的元跟踪器已标记为已完成。 OpenZFS 2.2 发布了第 3 个 RC 版本。 之前的 OpenZFS 2.2 候选版本已致力于实现与 Linux 6.4 内核的兼容性&#x…

Vue3 引用第三方Swiper内容触摸滑动简单应用

去官网查看更多教程→&#xff1a;Swiper官网 → 点击教程在vue中使用Swiper→ 在Vue中使用Swiper cd 到项目 安装Swiper&#xff1a; cnpm install --save swiper 安装指定版本 cnpm install --save swiper8.1.6 9.4.1 10.1.0…

SpringBoot-Hello World

SpringBootWeb快速入门 创建Springboot工程&#xff0c;并勾选web开发相关依赖定义HelloController类&#xff0c;添加方法hello&#xff0c;并添加相关注释运行测试 创建新的SpringBoot项目 几个注意的点&#xff1a; Name&#xff1a;基本上不用管&#xff0c;会根据下面的Ar…

关于elementui的input的autocomplete的使用

项目中需要实现搜索框搜索时能自动提示可选项的功能&#xff0c;elementui的input组件有已经封装好的el-autocomplete可以使用&#xff0c;但是在使用中发现一些问题&#xff0c;记录一下 基础使用 // html部分 <el-autocompletev-model"name":fetch-suggestion…

大数据课程I3——Kafka的消息流与索引机制

文章作者邮箱:yugongshiye@sina.cn 地址:广东惠州 ▲ 本章节目的 ⚪ 掌握Kafka的消息流处理; ⚪ 掌握Kafka的索引机制; ⚪ 掌握Kafka的消息系统语义; 一、Kafka消息流处理 1. Producer 写入消息 流程说明: 1. producer 要向Kafka生产消息,需要先通过…

EditPlus连接Linux系统远程操作文件

EditPlus是一套功能强大的文本编辑器&#xff01; 1.File ->FTP->FTP Settings&#xff1b; 2.Add->Description->FTP server->Username->Password->Subdirectory->Advanced Options 注意&#xff1a;这里的Subdirectory设置的是以后上传文件的默认…

20230814让惠普(HP)锐14 新AMD锐龙电脑不联网进WIN11进系统

20230814让惠普(HP)锐14 新AMD锐龙电脑不联网进WIN11进系统 2023/8/14 17:19 win11系统无法跳过联网 https://www.xpwin7.com/jiaocheng/28499.html Win11开机联网跳过不了怎么办&#xff1f;Win11开机联网跳过不了解决方法 Win11开机联网跳过不了怎么办&#xff1f;Win11开机…

Unity框架学习--5 事件中心管理器

作用&#xff1a;访问其它脚本时&#xff0c;不直接访问&#xff0c;而是通过发送一条“命令”&#xff0c;让监听了这条“命令”的脚本自动执行对应的逻辑。 原理&#xff1a; 1、让脚本向事件中心添加事件&#xff0c;监听对应的“命令”。 2、发送“命令”&#xff0c;事件…

【Sklearn】基于支持向量机算法的数据分类预测(Excel可直接替换数据)

【Sklearn】基于支持向量机算法的数据分类预测(Excel可直接替换数据) 1.模型原理1.1 数学模型1.2 模型原理2.模型参数3.文件结构4.Excel数据5.下载地址6.完整代码7.运行结果1.模型原理 支持向量机(Support Vector Machine,SVM)是一种用于分类和回归的监督学习算法,其基本…

【Git】安装以及基本操作

目录 一、初识Git二、 在Linux底下安装Git一&#xff09;centOS二&#xff09;Ubuntu 三、 Git基本操作一&#xff09; 创建本地仓库二&#xff09;配置本地仓库三&#xff09;认识工作区、暂存区、版本库四&#xff09;添加文件五&#xff09;查看.git文件六&#xff09;修改文…

基于docker部署的Selenium Grid分布式自动化测试

01、什么是Selenium Grid Selenium Grid是Selenium套件的一部分&#xff0c;它专门用于并行运行多个测试用例在不同的浏览器、操作系统和机器上。 Selenium Grid有两个版本——老版本Grid 1和新版本Grid 2。我们只对新版本做介绍&#xff0c;因为Selenium团队已经逐渐遗弃老版…

docker容器管理

创建容器&#xff1a; docker run --name 容器名 -d -p 端口1:端口2 –name :是启动容器时&#xff0c;给容器定义的名称&#xff0c;不使用该参数时&#xff0c;容器启动成功之后&#xff0c;会生成随机名称 -d &#xff1a;代表容器处于后台yunx -p &#xff1a;指定容器的端…

第一次参加计算机会议报告注意事项以及心得

计算机会议参会报告 注意事项参会前参会中参会后 参会心得 注意事项 接下来的会议注意事项分为&#xff1a;&#xff08;1&#xff09;参会前&#xff0c;&#xff08;2&#xff09;参会中&#xff0c;&#xff08;3&#xff09;参会后 参会前 参会前&#xff0c;一般被邀请…