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;
}
相关推荐
海盗猫鸥18 分钟前
C++入门基础篇(1)
开发语言·c++·学习
Thunter_3 小时前
[QT入门]树形视图控件
开发语言·c++·qt
Chris-zz3 小时前
C++:继承
开发语言·c++·算法·学习方法
川爻3 小时前
String类(STL开始)
开发语言·c++
fredricen3 小时前
CentOS7.9下yum升级Apache HTTP Server2.4.6到2.4.60
网络协议·http·apache
取加若则_4 小时前
C++入门(C语言过渡)
c语言·开发语言·数据结构·c++·算法
YangZheng@4 小时前
23种设计模式
c++·算法·设计模式
枫yy4 小时前
C++入门 容器适配器 / stack && queue模拟实现
开发语言·c++
溪渣渣_梁世华5 小时前
Qt 进程间通信(一)——QSharedMemory共享内存
c++·qt·进程间通信
程序员爱德华5 小时前
C++(week10):C++基础: 第二章 类与对象
c++·类和对象