首先安装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开发库:
sudo apt-get install libcurl4-openssl-dev
sudo yum install curl-devel 或 dnf install curl-devel
vcpkg install curl
brew install curl
编译时需链接curl库,例如g++命令:
g++ main.cpp -lcurl -o request
立即学习“C++免费学习笔记(深入)”;
以下代码演示如何使用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请求时,需要设置请求体和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且出现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查看详细日志。
以上就是c++怎么使用libcurl库发送HTTP请求_C++中用libcurl实现HTTP网络通信示例的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号