C/C++使用Curl发起Http请求
一、概述
使用curl发起http的get和pos请求示例
二、代码示例
1.封装Httpurl.h
#include <stdio.h> #include <iostream> #include <string> #include <string.h> #include "json/json.h" #include <fstream> extern "C" { #include <curl/curl.h> } class HttpCurl { private: //创建一个Curl句柄 CURL* curl; CURLcode res; std::string baseUrl; public: HttpCurl(); ~HttpCurl(); public: void init(); void setBaseUrl(std::string baseUrl); void get(std::string url); //void get(); void post(std::string url,std::string bodyJson); //void post(); void destory(); };
2.HttpCurl.cpp
#include "m_curl.h" HttpCurl::HttpCurl() { //init(); } HttpCurl::~HttpCurl() { } void HttpCurl::init() { //初始化curl库 curl_global_init(CURL_GLOBAL_ALL); } void HttpCurl::setBaseUrl(std::string baseUrl) { this->baseUrl = baseUrl; } size_t write_callback(void* ptr, size_t size, size_t nmemb, void* stream) { //在注释的里面可以打印请求流,cookie的信息 //cout << "----->reply" << endl; std::string* str = (std::string*)stream; std::cout << *str << std::endl; (*str).append((char*)ptr, size * nmemb); std::cout << "callback is ......" << *str << std::endl; return size * nmemb; } //get请求 void HttpCurl::get(std::string url) { std::string httpUrl = this->baseUrl.append(url); std::cout << "请求接口=" << httpUrl << std::endl; //初始化curl句柄 curl = curl_easy_init(); //给句柄设置参数(封装一个http请求) curl_easy_setopt(curl, CURLOPT_URL, httpUrl.c_str()); //设置http请求内容类型为json格式 curl_slist* plist = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8"); //设置token curl_slist_append(plist, "Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MjExMTg3MDYsInVzZXJuYW1lIjoiMTY5MTYzMTk0OTQyMyJ9.t8_e-kRYlmySUytIAj3Vp93YUoDBoXNzfd28dq2koQ0"); //设置businessid curl_slist_append(plist, "businessId:1794906728071634946"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist); //给当前句柄设置一个从服务器返回数据的回调 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //给回调函数设置一个形参 std::string response; curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&response); //提交请求,阻塞等待服务器返回 res = curl_easy_perform(curl); if (res != CURLE_OK) { printf("curl easy perform error res = %d\n", res); std::cout << "请求发生了一场" << std::endl; } std::cout << "response:" << response << std::endl; //处理服务器返回的数据 //清空、释放句柄内存空间 curl_easy_cleanup(curl); } void HttpCurl::post(std::string url, std::string bodyJson) { std::string httpUrl = this->baseUrl.append(url); std::cout << "url:" << httpUrl << std::endl; //初始化curl句柄 curl = curl_easy_init(); //给句柄设置参数(封装一个http请求) curl_easy_setopt(curl, CURLOPT_URL, httpUrl.c_str()); //设置http请求内容类型为json格式 curl_slist* plist = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8"); ////设置token //curl_slist_append(plist, "Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MjExMTg3MDYsInVzZXJuYW1lIjoiMTY5MTYzMTk0OTQyMyJ9.t8_e-kRYlmySUytIAj3Vp93YUoDBoXNzfd28dq2koQ0"); ////设置businessid //curl_slist_append(plist, "businessId:1794906728071634946"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist); curl_easy_setopt(curl, CURLOPT_POST, 1L); // POST //std::cout <<"----=" << result << std::endl; //printf("--------------%s\n", postJson.toStyledString().c_str()); /*std::ofstream ofs; ofs.open("C:/Users/DBF-DEV-103/Downloads/curl_json_text.txt",std::ios::out); ofs << result << std::endl; ofs.close();*/ //jsoncpp //这里有点不正常,如果直接使用postJson.toStyledString().c_str()后台无法解析,如果先把值赋值给std::string此方法就能行得通 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, bodyJson.c_str()); curl_easy_setopt(curl, CURLOPT_ENCODING, "utf-8"); //给当前句柄设置一个从服务器返回数据的回调 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); //给回调函数设置一个形参 std::string response; curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&response); //提交请求,阻塞等待服务器返回 res = curl_easy_perform(curl); if (res != CURLE_OK) { printf("curl easy perform error res = %d\n", res); std::cout << "请求发生了异常" << std::endl; } std::cout << "response:" << response << std::endl; //处理服务器返回的数据 //清空、释放句柄内存空间 curl_easy_cleanup(curl); } void HttpCurl::destory() { curl_global_cleanup();//清除curl,释放内存 }
3.使用HttpCurl发起get和post请求
SetConsoleOutputCP(CP_UTF8); // 设置控制台输出为 UTF-8 编码 std::cout << "start" << std::endl; HttpCurl httpCurl; //测试get请求 /*std::string baseUrl = "http://192.168.100.201:8080/"; httpCurl.setBaseUrl(baseUrl); httpCurl.get("api/get/userinfo");*/ //测试post请求 std::string baseUrl = "http://192.168.190.164:8080/"; httpCurl.setBaseUrl(baseUrl); Json::Value postJson; postJson["username"] = "admin"; postJson["password"] = "123456"; std::string result = postJson.toStyledString(); httpCurl.post("api/user/login", result);