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;
}
相关推荐
肆忆_1 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
不可能的是1 天前
前端 SSE 流式请求三种实现方案全解析
前端·http
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马5 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝5 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc5 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法