首先安装libcurl库并链接编译,然后通过curl_easy_init初始化,设置CURLOPT_URL等选项,使用CURLOPT_WRITEFUNCTION回调接收数据,GET请求直接执行,POST请求需设置CURLOPT_POSTFIELDS和HTTP头,HTTPS可关闭验证或指定CA证书路径。

在C++中使用libcurl发送HTTP请求,是实现网络通信的常见方式。libcurl是一个功能强大、跨平台的客户端URL传输库,支持HTTP、HTTPS、FTP等多种协议。下面介绍如何配置并使用libcurl发送GET和POST请求。
安装与配置libcurl
在开始前,确保系统已安装libcurl开发库:
-
Ubuntu/Debian: 执行
sudo apt-get install libcurl4-openssl-dev -
CentOS/Fedora: 使用
sudo yum install curl-devel或dnf install curl-devel -
Windows (MSVC): 可通过vcpkg安装:
vcpkg install curl -
macOS: 使用Homebrew:
brew install curl
编译时需链接curl库,例如g++命令:
g++ main.cpp -lcurl -o request
发送GET请求示例
以下代码演示如何使用libcurl发送一个简单的HTTP GET请求,并获取响应内容。
#include#include #include // 回调函数:接收响应数据 size_t WriteCallback(void contents, size_t size, size_t nmemb, std::string output) { size_t totalSize = size nmemb; output->append((char)contents, totalSize); return totalSize; }
int main() { CURL* curl; CURLcode res; std::string readBuffer;
curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://httpbin.org/get"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 设置超时 res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr zuojiankuohaophpcnzuojiankuohaophpcn "请求失败: " zuojiankuohaophpcnzuojiankuohaophpcn curl_easy_strerror(res) zuojiankuohaophpcnzuojiankuohaophpcn std::endl; } else { std::cout zuojiankuohaophpcnzuojiankuohaophpcn "响应内容:\n" zuojiankuohaophpcnzuojiankuohaophpcn readBuffer zuojiankuohaophpcnzuojiankuohaophpcn std::endl; } curl_easy_cleanup(curl); } return 0;}
发送POST请求(JSON数据)
发送POST请求时,需要设置请求体和Content-Type头。
#include#include #include size_t WriteCallback(void contents, size_t size, size_t nmemb, std::string output) { size_t totalSize = size nmemb; output->append((char)contents, totalSize); return totalSize; }
int main() { CURL* curl; CURLcode res; std::string readBuffer; std::string postData = R"({"name": "Alice", "age": 25})";
curl = curl_easy_init(); if (curl) { struct curl_slist* headers = nullptr; headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_URL, "https://httpbin.org/post"); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.length()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); res = curl_easy_perform(curl); if (res != CURLE_OK) { std::cerr zuojiankuohaophpcnzuojiankuohaophpcn "POST请求失败: " zuojiankuohaophpcnzuojiankuohaophpcn curl_easy_strerror(res) zuojiankuohaophpcnzuojiankuohaophpcn std::endl; } else { std::cout zuojiankuohaophpcnzuojiankuohaophpcn "POST响应:\n" zuojiankuohaophpcnzuojiankuohaophpcn readBuffer zuojiankuohaophpcnzuojiankuohaophpcn std::endl; } curl_slist_free_all(headers); curl_easy_cleanup(curl); } return 0;}
处理HTTPS请求与证书验证
若目标网站使用HTTPS且出现SSL证书错误,可临时关闭证书验证(仅用于测试):
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);注意:生产环境中不建议关闭证书验证,应配置正确的CA证书路径:
curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/cacert.pem");基本上就这些。掌握libcurl的核心用法后,你可以轻松扩展到上传文件、设置Cookie、处理重定向等高级功能。关键在于理解选项设置和回调机制。调试时多用
CURLOPT_VERBOSE查看详细日志。











