首先安装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
立即学习“C++免费学习笔记(深入)”;
发送GET请求示例
以下代码演示如何使用libcurl发送一个简单的HTTP GET请求,并获取响应内容。
#include <iostream>
#include <string>
#include <curl/curl.h>
<p>// 回调函数:接收响应数据
size_t WriteCallback(void<em> contents, size_t size, size_t nmemb, std::string</em> output) {
size_t totalSize = size <em> nmemb;
output->append((char</em>)contents, totalSize);
return totalSize;
}</p><p>int main() {
CURL* curl;
CURLcode res;
std::string readBuffer;</p><pre class='brush:php;toolbar:false;'>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 << "请求失败: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "响应内容:\n" << readBuffer << std::endl;
}
curl_easy_cleanup(curl);
}
return 0;
}
发送POST请求(JSON数据)
发送POST请求时,需要设置请求体和Content-Type头。
#include <iostream>
#include <string>
#include <curl/curl.h>
<p>size_t WriteCallback(void<em> contents, size_t size, size_t nmemb, std::string</em> output) {
size_t totalSize = size <em> nmemb;
output->append((char</em>)contents, totalSize);
return totalSize;
}</p><p>int main() {
CURL* curl;
CURLcode res;
std::string readBuffer;
std::string postData = R"({"name": "Alice", "age": 25})";</p><pre class='brush:php;toolbar:false;'>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 << "POST请求失败: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "POST响应:\n" << readBuffer << 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查看详细日志。











