環(huán)境配置
curl //DV2020T環(huán)境下此步驟可省略
https://curl.se/download/
筆者安裝為7.85.0版本
./configure --without-ssl
make
sudo make install
sudo rm /usr/local/lib/curl
系統(tǒng)也有curl庫,為防止沖突,刪去編譯好的curl庫。
對以json數(shù)據(jù)的解析使用開源項目:https://github.com/nlohmann/json
cd single_include 在這個文件夾里有json.hpp文件,我們只需要包含這一個頭文件即可,它不能編譯,更沒有庫。
Makefile文件
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請求文章來源:http://www.zghlxwxcb.cn/news/detail-838987.html
#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 表示收到服務器返回數(shù)據(jù)的首地址
* size 表示返回每個數(shù)據(jù)的大小
* nmemb 表示返回數(shù)據(jù)的個數(shù)
* userdata 用戶給該回調(diào)函數(shù)傳遞的形參 curl_easy_setopt(curl, CURLOPT_WRITEDATA, "abc"); 設置的字符串"abc"
* 這個可以用來標識傳輸命令 返回的數(shù)據(jù) 來自命令 "abc",根據(jù)這個命令來處理這個數(shù)據(jù)
*/
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. 創(chuàng)建一個curl句柄
CURL* curl = nullptr;
CURLcode res;
//2. 初始化一個curl句柄
curl = curl_easy_init();
//3. 給該句柄設定一些參數(shù) (封裝一個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");
//給當前句柄設置一個 處理從服務器返回數(shù)據(jù)的回調(diào)函數(shù)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //TODO:
string response;
//給回調(diào)函數(shù)傳遞一個形參
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. 處理服務器返回數(shù)據(jù)
//6. 清空 釋放句柄內(nèi)存空間
curl_easy_cleanup(curl);
return 0;
}
DELETE請求文章來源地址http://www.zghlxwxcb.cn/news/detail-838987.html
#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 表示收到服務器返回數(shù)據(jù)的首地址
* size 表示返回每個數(shù)據(jù)的大小
* nmemb 表示返回數(shù)據(jù)的個數(shù)
* userdata 用戶給該回調(diào)函數(shù)傳遞的形參 curl_easy_setopt(curl, CURLOPT_WRITEDATA, "abc"); 設置的字符串"abc"
* 這個可以用來標識傳輸命令 返回的數(shù)據(jù) 來自命令 "abc",根據(jù)這個命令來處理這個數(shù)據(jù)
*/
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. 創(chuàng)建一個curl句柄
CURL* curl = nullptr;
CURLcode res;
//2. 初始化一個curl句柄
curl = curl_easy_init();
//3. 給該句柄設定一些參數(shù) (封裝一個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
//給當前句柄設置一個 處理從服務器返回數(shù)據(jù)的回調(diào)函數(shù)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //TODO:
string response;
//給回調(diào)函數(shù)傳遞一個形參
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. 處理服務器返回數(shù)據(jù)
json jsonContent = json::parse(response);
string cid = jsonContent["id"];
//6. 清空 釋放句柄內(nèi)存空間
curl_easy_cleanup(curl);
return 0;
}
到了這里,關于curl c++ 實現(xiàn)HTTP GET和POST請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!