libcurl的get、post的使用

demo使用的是curl-8.3.0.tar.gz,其它版本也可以,安装教程可以去网上搜

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
/* somewhat unix-specific */
#include <sys/time.h>
#include <unistd.h>/* curl stuff */
#include <curl/curl.h>
#include <curl/mprintf.h>#define HTTP_DEFAULT_CONN_TIME		(30)		// 30s,http connect timeout
#define COOKIE_MAX_LEN				(100)		//http cookie string len max
#define MAX_URL_SIZE		        (1024)      // url最大长度
#define MAX_DATA_SIZE		        (1024)      // url最大长度
#define SFHTTP_OPERATION_TIMEOUT	(60)		// default operation time out 60s
#define SFHTTP_CONNECT_TIMEOUT		(30)		// default connect time out 30s
#define SFHTTP_DOWNLOAD_TIMEOUT		(600)		// 600s
#define SFHTTP_HEADER_EXPECT		"Expect:"	// continue 
#define DOWN_FILE_PATH				"./agent.deb"
// #define _DEBUG#ifdef _DEBUGstatic 
int 
OnDebug(CURL *handle, curl_infotype itype, char *pData, size_t size, void *userp)
{if (itype == CURLINFO_TEXT) {printf("[TEXT]%s", pData);}if (itype == CURLINFO_HEADER_IN) {printf("[HEADER_IN]%s", pData);}if (itype == CURLINFO_HEADER_OUT) {printf("[HEADER_OUT]%s", pData);}if (itype == CURLINFO_DATA_IN) {printf("[DATA_IN]%s", pData);}if (itype == CURLINFO_DATA_OUT) {printf("[DATA_OUT]%s", pData);}return 0;
}#endifstatic 
size_t
OnWriteData(char * pResponseStream,size_t size,size_t nmemb,void* pContext)
{char *pStr = NULL;assert(pContext != NULL);pStr = (char *)pContext;// pStr->append((char*)pResponseStream, size * nmemb);// snprintf(pStr, size * nmemb, "%s", pResponseStream);strncpy(pStr, pResponseStream, size * nmemb);return size * nmemb;
}static
size_t 
OnDownWriteData(char *ptr, size_t uByte, size_t nmemb, void *userdata) 
{FILE *pFile = NULL;size_t retBytes = 0;pFile = (FILE*)userdata;if (userdata == NULL) {return 0;}retBytes = fwrite(ptr, 1, (uByte * nmemb), pFile);if (retBytes == 0 || retBytes != (uByte * nmemb)) {return 0;}return (size_t)retBytes;
}void http_get(){CURL* pCurl = NULL;int iRet = 0;long lRespCode = 0;size_t uConnectime = 0, uOprtime = 0;char *pszData = NULL;CURLcode curlCode = CURLE_OK;char s_uid_cookie[COOKIE_MAX_LEN];char pszUrl[MAX_URL_SIZE] = {0};char strData[MAX_DATA_SIZE] = {0};snprintf(pszUrl, sizeof(pszUrl), "%s", "http://10.74.37.146:9999/ping");pCurl = curl_easy_init();if (NULL == pCurl) {fprintf(stderr,"curl_easy_init or curl_slist_append failed!\n");goto L_Cleanup;}iRet |= curl_easy_setopt(pCurl, CURLOPT_URL, pszUrl);// if(NULL != pszUid)// {// 	if(strlen(pszUid) >= 8)// 	{// 		str_snprintf(s_uid_cookie, COOKIE_MAX_LEN, "company_code=%s", pszUid);// 		iRet |= curl_easy_setopt(pCurl, CURLOPT_COOKIE, s_uid_cookie);// 	}// }uConnectime = SFHTTP_CONNECT_TIMEOUT;uOprtime = SFHTTP_OPERATION_TIMEOUT;iRet |= curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, uConnectime);iRet |= curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uOprtime);#ifdef _DEBUG iRet |= curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif iRet |= curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, NULL);iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteData);iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void *)strData);iRet |= curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0);iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0);if (iRet != 0) {fprintf(stderr,"curl_easy_setopt failed with err = %d!", iRet);goto L_Cleanup;}curlCode = curl_easy_perform(pCurl);if (curlCode != CURLE_OK) {fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));goto L_Cleanup;}curlCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &lRespCode);if (curlCode != CURLE_OK) {fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));goto L_Cleanup;}printf("recv Data: %s \n", strData);L_Cleanup:if (pCurl) {curl_easy_cleanup(pCurl);pCurl = NULL;}
}void http_post(){CURL* pCurl = NULL;int iRet = 0;long lRespCode = 0;size_t uConnectime = 0, uOprtime = 0;char *pszData = NULL;CURLcode curlCode = CURLE_OK;struct curl_slist * pHeader = NULL;char s_uid_cookie[COOKIE_MAX_LEN];char pszUrl[MAX_URL_SIZE] = {0};char strData[MAX_DATA_SIZE] = {0};snprintf(pszUrl, sizeof(pszUrl), "%s", "http://10.74.37.146:9999/submit");pCurl = curl_easy_init();if (NULL == pCurl) {fprintf(stderr,"curl_easy_init or curl_slist_append failed!\n");goto L_Cleanup;}pHeader = curl_slist_append(pHeader, "Content-Type: application/json");if (pHeader == NULL) {fprintf(stderr, "curl_easy_init or curl_slist_append failed!\n");goto L_Cleanup;}iRet |= curl_easy_setopt(pCurl, CURLOPT_URL, pszUrl);uConnectime = SFHTTP_CONNECT_TIMEOUT;uOprtime = SFHTTP_OPERATION_TIMEOUT;iRet |= curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, uConnectime);iRet |= curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uOprtime);#ifdef _DEBUG iRet |= curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif // iRet |= curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);// iRet |= curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0);iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0);iRet |= curl_easy_setopt(pCurl, CURLOPT_POST, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnWriteData);iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, (void *)strData);iRet |= curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, NULL);// iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "name=hds&email=curl");iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "{\"name\":\"hds\",\"email\":\"curl@qq.com\"}");iRet |= curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pHeader);if (iRet != 0) {fprintf(stderr,"curl_easy_setopt failed with err = %d!", iRet);goto L_Cleanup;}curlCode = curl_easy_perform(pCurl);if (curlCode != CURLE_OK) {fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));goto L_Cleanup;}curlCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &lRespCode);if (curlCode != CURLE_OK) {fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));goto L_Cleanup;}printf("recv Data: %s \n", strData);L_Cleanup:if (pCurl) {curl_easy_cleanup(pCurl);pCurl = NULL;}
}void http_down_file(){CURL* pCurl = NULL;int iRet = 0;long lRespCode = 0;size_t uConnectime = 0, uOprtime = 0;char *pszData = NULL;CURLcode curlCode = CURLE_OK;struct curl_slist * pHeader = NULL;char s_uid_cookie[COOKIE_MAX_LEN];char pszUrl[MAX_URL_SIZE] = {0};// char strData[MAX_DATA_SIZE] = {0};char pszFilePath[MAX_URL_SIZE] = {0};/* 创建文件 */FILE* pFile = NULL;strncpy(pszFilePath, DOWN_FILE_PATH, strlen(DOWN_FILE_PATH));pFile = fopen(pszFilePath, "wb");if (pFile == NULL) {fprintf(stderr,"fopen %s failed with error code = %d", pszFilePath, errno);goto L_Cleanup;}snprintf(pszUrl, sizeof(pszUrl), "%s", "http://10.74.37.146:9999/down_file");pCurl = curl_easy_init();if (NULL == pCurl) {fprintf(stderr,"curl_easy_init or curl_slist_append failed!\n");goto L_Cleanup;}pHeader = curl_slist_append(pHeader, "Content-Type: application/json");if (pHeader == NULL) {fprintf(stderr, "curl_easy_init or curl_slist_append failed!\n");goto L_Cleanup;}iRet |= curl_easy_setopt(pCurl, CURLOPT_URL, pszUrl);uConnectime = SFHTTP_CONNECT_TIMEOUT;uOprtime = SFHTTP_OPERATION_TIMEOUT;iRet |= curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, uConnectime);iRet |= curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uOprtime);#ifdef _DEBUG iRet |= curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, OnDebug);
#endif iRet |= curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1);// iRet |= curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0);iRet |= curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0);iRet |= curl_easy_setopt(pCurl, CURLOPT_POST, 1);iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OnDownWriteData);iRet |= curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pFile);iRet |= curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, NULL);iRet |= curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_LIMIT , 1); // 最低下载速度 1byte/sec,超过10min断开链接iRet |= curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_TIME , SFHTTP_DOWNLOAD_TIMEOUT);// iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "name=hds&email=curl");iRet |= curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, "{\"file_name\":\"agent.deb\"}");iRet |= curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pHeader);if (iRet != 0) {fprintf(stderr,"curl_easy_setopt failed with err = %d!", iRet);goto L_Cleanup;}curlCode = curl_easy_perform(pCurl);if (curlCode != CURLE_OK) {fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));goto L_Cleanup;}curlCode = curl_easy_getinfo(pCurl, CURLINFO_RESPONSE_CODE, &lRespCode);if (curlCode != CURLE_OK) {fprintf(stderr, "curl_easy_perform failed msg: %s\n", curl_easy_strerror(curlCode));goto L_Cleanup;}// printf("recv Data: %s \n", strData);L_Cleanup:if(pFile){fclose(pFile);}if (pCurl) {curl_easy_cleanup(pCurl);pCurl = NULL;}
}// 编译:gcc -o curl_get curl_get.c -lcurl -I/root/code/dynamic_so/curl/include -L/root/code/dynamic_so/curl/lib
int main(){printf("hello world \n");http_down_file();return 0;
}

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

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

相关文章

最新GPT4.0使用教程,AI绘画,ChatFile文档对话总结+GPT语音对话使用,DALL-E3文生图

一、前言 ChatGPT3.5、GPT4.0、GPT语音对话、Midjourney绘画&#xff0c;文档对话总结DALL-E3文生图&#xff0c;相信对大家应该不感到陌生吧&#xff1f;简单来说&#xff0c;GPT-4技术比之前的GPT-3.5相对来说更加智能&#xff0c;会根据用户的要求生成多种内容甚至也可以和…

16.顺子日期(14)

题目 public class Main {public static boolean isLegal(String date) {int l 0;int n date.length();while(l<(n-3)) {int t1 (int)Integer.valueOf(date.substring(l,l1));int t2 (int)Integer.valueOf(date.substring(l1,l2));int t3 (int)Integer.valueOf(date.s…

FreeRTOS 实时操作系统第十二讲 - 计数信号量

一、信号量的概念 1、信号量的基本概念 消息队列是实现任务与任务或任务与中断间通信的数据结构&#xff0c;可类比裸机编程中的数组 信号量是实现任务与任务或任务与中断间通信的机制&#xff0c;可以类比裸机编程中的标志位 信号量 (semaphore) 可以实现任务与任务或任务与中…

C#的StringBuilder属性

目录 一、StringBuilder构造器 二、StringBuilder属性 1.StringBuilder.Capacity 属性 2.StringBuilder.Chars[Int32] 属性 &#xff08;1&#xff09;定义 &#xff08;2&#xff09;实例 3.StringBuilder.Length 属性 &#xff08;1&#xff09;定义 &#xff08;2&…

银河麒麟Kylin-Server-V10-SP3使用ISO镜像搭建本地内网YUM/DNF源cdrom/http

机房服务器安装一般是内网环境&#xff0c;需要配置本地的YUM/DNF源。本文介绍通过ISO镜像搭建内网环境的UM/DNF源 准备工作&#xff1a; 提前准备好Kylin-Server-V10-SP3的ISO镜像文件。 本机IP地址&#xff1a;192.168.40.201 镜像存放目录/data/iso/Kylin-Server-V10-SP3-Ge…

Linux-端口、nmap命令、netstat命令

端口是设备与外界通讯交流的出入口&#xff0c;可分为物理端口和虚拟端口 物理端口实际存在可以看见&#xff0c;而虚拟端口是指计算机内部的端口&#xff0c;是不可见的&#xff0c;用来操作系统和外部交互使用。 IP地址不能锁定程序&#xff0c;所以可以通过端口&#xff0…

原生微信小程序如何动态修改svg图片颜色及尺寸、宽高(封装svgIcon组件)解决ios不显示问题

最终效果 前言 动态设置Svg图片颜色就是修改Svg源码的path中的fill属性&#xff0c; 通过wx.getFileSystemManager().readFile读取.xlsx文件 ios不显示需要把encoding设置 binary 把文件转成base64 封装svg-icon组件 1、在项目的components下新建svg-icon文件夹&#xff0c;新…

OA系统与工单系统的区别:功能、应用与优势对比

很多企业在选购系统的时候会陷入选择困难症的漩涡。市面上的各种系统&#xff0c;比如ERP系统、OA系统、工单系统等等让人眼花缭乱。想要选择一款合适的系统&#xff0c;前提是明确地知道自己需要用它来做什么&#xff0c;其次去了解每个系统的应用场景、功能等是否与自己的需求…

自动驾驶感知-预测-决策-规划-控制学习(3):感知方向文献阅读笔记

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、文章主题二、摘要阅读1.名词理解①点云是什么&#xff1f;②二维图像分割器③轻量化卷积网络提取特征④单模态表达和多模态特征融合的区别⑤基于ROS的多传感…

jetson deepstream 解码接入编码输出

不需要编解码输出画面的直接到7 使用就行 1 jetson主板编译工具 在jetson主板上安装gstreamer工具链&#xff0c;编译opencv sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-ugly gstreamer1.0-rtsp python3-dev pytho…

安全与认证Week3 Tutorial+历年题补充

目录 1) 什么是重放攻击? 2)什么是Kerberos系统?它提供什么安全服务? 3)服务器验证客户端身份的一种简单方法是要求提供密码。在Kerberos中不使用这种身份验证&#xff0c;为什么?Kerberos如何对服务器和客户机进行身份验证? 4) Kerberos的四个要求是什么?Kerberos系…

C++日期类的实现

前言&#xff1a;在类和对象比较熟悉的情况下&#xff0c;我们我们就可以开始制作日期表了&#xff0c;实现日期类所包含的知识点有构造函数&#xff0c;析构函数&#xff0c;函数重载&#xff0c;拷贝构造函数&#xff0c;运算符重载&#xff0c;const成员函数 1.日期类的加减…

JetBrains Rider使用总结

简介&#xff1a; JetBrains Rider 诞生于2016年&#xff0c;一款适配于游戏开发人员&#xff0c;是JetBrains旗下一款非常年轻的跨平台 .NET IDE。目前支持包括.NET 桌面应用、服务和库、Unity 和 Unreal Engine 游戏、Xamarin 、ASP.NET 和 ASP.NET Core web 等多种应用程序…

python基础教程七(print参数,import,序列解包,赋值,缩进等)

在学习if条件句以前我们先说点别的 1. priint 你知道&#xff0c;print用于打印一个表达式&#xff0c;这个表达式要么是字符串&#xff0c;要么将自动转换成字符串。实际上&#xff0c;你可以同时打印多个表达式,条件是用逗号分隔它们: >>> print(age:,42) age: 4…

大白话IDE:入门指南

什么是IDE&#xff1f; IDE&#xff0c;即集成开发环境&#xff08;Integrated Development Environment&#xff09;&#xff0c;是帮助程序员进行软件开发的应用程序。它通常包括代码编辑器、编译器、调试器和图形用户界面等工具。 为什么使用IDE&#xff1f; 省时省力&am…

面试题--消失的两个数字(困难)

个人主页&#xff1a;Lei宝啊 愿所有美好如期而遇 目录 本题链接 输入描述 输出描述 算法分析 触类旁通一&#xff1a;消失的数字 题目分析 图示 解题源码 触类旁通二&#xff1a;只出现一次的数字III 题目分析 图示 解题源码 本题分析 解题源码 本题链接 力…

Linux第19步_安装“Ubutun交叉编译工具链”

由于Ubuntu系统使用的GCC编译器&#xff0c;编译结果是X86文件&#xff0c;只能在X86上运行&#xff0c;不能在ARM上直接运行。因此&#xff0c;还要安装一个“Ubutun交叉编译工具链”&#xff0c;才可以在ARM上运行。 arm-none-linux-gnueabi-gcc是 Codesourcery 公司&#x…

PHP语言B/S架构医院(安全)不良事件上报系统源码

医院安全&#xff08;不良&#xff09;事件上报系统采用无责的、自愿的填报不良事件方式&#xff0c;有效地减轻医护人员的思想压力&#xff0c;实现以事件为主要对象&#xff0c;可以自动、及时、实际地反应医院的安全、不良、近失事件的情况&#xff0c;更好地掌握不良事件的…

【Docker】docker 服务相关命令

目录 1. 启动docker 服务 2.查看docker 服务的状态 3. 停止docker 服务 4.重启 docker 服务 5.开机自启动命令 1. 启动docker 服务 systemctl start docker 2.查看docker 服务的状态 systemctl status docker 3. 停止docker 服务 systemctl stop docker 此时再使用 syst…

树莓派 GPIO

1、GPIO简介 树莓派(GPIO)是一种数字输入/输出接口&#xff0c;可用于控制和监测电子元件。输入引脚可用于读取传感器或按键等外部设备输入的信号&#xff1b;输出引脚可用于通过外部设备火车、LED灯等输出电压或电流。在树莓派上&#xff0c;GPIO用于与外部世界进行通讯&…