curl c++ 实现HTTP GET和POST请求

环境配置

curl //DV2020T环境下此步骤可省略

https://curl.se/download/

笔者安装为7.85.0版本

./configure --without-ssl

make

sudo make install

sudo rm /usr/local/lib/curl

系统也有curl库,为防止冲突,删去编译好的curl库。

对以json数据的解析使用开源项目:https://github.com/nlohmann/json

cd single_include 在这个文件夹里有json.hpp文件,我们只需要包含这一个头文件即可,它不能编译,更没有库。

Makefile文件

cpp 复制代码
CC=g++
SDK_PATH=./include
CFLAGS=-Wno-multichar -I $(SDK_PATH) -fno-rtti
LDFLAGS=-lm -ldl -lpthread -std=c++11 -lcurl

HEADERS= \
SRCS= main.cpp\
HTTP: $(SRCS) $(HEADERS) 
	$(CC) -o  HTTP $(SRCS)  $(CFLAGS) $(LDFLAGS) -g
clean:
	rm -f HTTP

GET请求

cpp 复制代码
#include "curl/curl.h"
//get请求
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <unistd.h>
#include "nlohmann/json.hpp"
using json = nlohmann::json;
using namespace  std;

/*
* ptr      表示收到服务器返回数据的首地址
* size     表示返回每个数据的大小
* nmemb    表示返回数据的个数
* userdata 用户给该回调函数传递的形参   curl_easy_setopt(curl, CURLOPT_WRITEDATA, "abc"); 设置的字符串"abc"
*          这个可以用来标识传输命令 返回的数据 来自命令 "abc",根据这个命令来处理这个数据
*/

size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
	//在注释的里面可以打印请求流,cookie的信息
	//cout << "----->reply" << endl;
	string *str = (string*)stream;
	//cout << *str << endl;
	(*str).append((char*)ptr, size*nmemb);
	return size * nmemb;
}


int main(void) {

	//1. 创建一个curl句柄
	CURL* curl = nullptr;
	CURLcode res;

	//2. 初始化一个curl句柄
	curl = curl_easy_init();

	//3. 给该句柄设定一些参数 (封装一个http请求消息)  "127.0.0.1", "/login", "id=liukang&pw=123"
	// curl_easy_setopt(curl, CURLOPT_URL, "http://39.98.187.101:1985/api/v1/clients/?count=10"); //http://www.baidu.com //get
	curl_easy_setopt(curl, CURLOPT_URL, "http://39.98.187.101:1985/api/v1/clients/y2o21qc7"); //http://www.baidu.com

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
	//给当前句柄设置一个 处理从服务器返回数据的回调函数
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //TODO:
	string response;
	//给回调函数传递一个形参
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
	
	//4. 将curl句柄 向远程服务器 提交请求 并得到一个返回值
	res = curl_easy_perform(curl);  //阻塞等待服务器返回
	if(res != CURLE_OK) {
		printf("curl easy perform error res = %d\n", res);
		return 1;
	}
	sleep(2);
	cout << "response : " << response << endl;
	//5. 处理服务器返回数据

	//6. 清空 释放句柄内存空间
	curl_easy_cleanup(curl);

	return 0;
}

DELETE请求

cpp 复制代码
#include "curl/curl.h"
//get请求
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <unistd.h>
using namespace  std;

/*
* ptr      表示收到服务器返回数据的首地址
* size     表示返回每个数据的大小
* nmemb    表示返回数据的个数
* userdata 用户给该回调函数传递的形参   curl_easy_setopt(curl, CURLOPT_WRITEDATA, "abc"); 设置的字符串"abc"
*          这个可以用来标识传输命令 返回的数据 来自命令 "abc",根据这个命令来处理这个数据
*/

size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
	//在注释的里面可以打印请求流,cookie的信息
	//cout << "----->reply" << endl;
	string *str = (string*)stream;
	//cout << *str << endl;
	(*str).append((char*)ptr, size*nmemb);
	return size * nmemb;
}


int main(void) {

	//1. 创建一个curl句柄
	CURL* curl = nullptr;
	CURLcode res;

	//2. 初始化一个curl句柄
	curl = curl_easy_init();

	//3. 给该句柄设定一些参数 (封装一个http请求消息)  "127.0.0.1", "/login", "id=liukang&pw=123"
	curl_easy_setopt(curl, CURLOPT_URL, "http://39.98.187.101:1985/api/v1/clients/"); //http://www.baidu.com //get

	//给当前句柄设置一个 处理从服务器返回数据的回调函数
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //TODO:
	string response;
	//给回调函数传递一个形参
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (string*)&response);

	//4. 将curl句柄 向远程服务器 提交请求 并得到一个返回值
	res = curl_easy_perform(curl);  //阻塞等待服务器返回
	if(res != CURLE_OK) {
		printf("curl easy perform error res = %d\n", res);
		return 1;
	}
	sleep(2);
	cout << "response : " << response << endl;
	//5. 处理服务器返回数据
json jsonContent = json::parse(response);
string cid = jsonContent["id"];
	//6. 清空 释放句柄内存空间
	curl_easy_cleanup(curl);

	return 0;
}
相关推荐
程序员编程指南27 分钟前
Qt XML 与 JSON 数据处理方法
xml·c语言·c++·qt·json
Algebraaaaa1 小时前
【C++基础】指针常量 | 常量指针 | int* p | const int* p | int* const p| const int* const p
c++
祁同伟.1 小时前
【C++】类和对象(中)构造函数、析构函数
开发语言·c++
郝学胜-神的一滴2 小时前
C++ 类型萃取:深入理解与实践
开发语言·c++·程序人生
喵手2 小时前
Java 11 新特性:从模块化到 HTTP/2 深度解析
java·开发语言·http
程序员编程指南2 小时前
Qt 网络编程进阶:网络安全与加密
c语言·网络·c++·qt·web安全
GOATLong3 小时前
传输层协议TCP
c语言·开发语言·网络·c++·网络协议·tcp/ip
天若有情6734 小时前
从一个“诡异“的C++程序理解状态机、防抖与系统交互
开发语言·c++·交互·面向对象·状态机
bilin_jam4 小时前
C++查询mysql数据
数据库·c++·mysql
JNU freshman5 小时前
C++ 常用的数据结构(适配器容量:栈、队列、优先队列)
数据结构·c++