Win10上Qt使用Libcurl库
第1章 下载libcurl
下载链接
第2章 curl集成到Qt中
- 将已经编译好的libcurl目录下的lib和include目录拷贝到项目源码目录中。
![]() |
![]() |
- 将libcurl目录下的bin目录下的【libcurl-x64.dll】动态库拷贝到源码目录下可执行文件【testdemo3.exe】位置。
第3章 配置qmake
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.h# 设置 libcurl 的头文件路径
INCLUDEPATH += E:/Mytest/test20250814_1/testdemo3/include# 设置 libcurl 的库文件路径
LIBS += -L"E:/Mytest/test20250814_1/testdemo3/lib"# 链接curl的主库 libcurl.a 静态库
LIBS += -lcurl# 链接 libssl.a 和 libcrypto.a。以便支持OpenSSL,用于HTTPS
LIBS += -lssl
LIBS += -lcrypto# 链接 libz.a。以便支持zlib,用于 HTTP 压缩(gzip)
LIBS += -lz# 链接 libssh2.a。以便支持SSH/SFTP 支持
LIBS += -lz# 定义宏,表示使用静态库
DEFINES += CURL_STATICLIB# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
第4章 访问百度测试
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <string>// 引入 libcurl
extern "C" {
#include <curl/curl.h>
}class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void onFetchBaidu(); // 按钮点击事件private:QPushButton *fetchButton;// libcurl 回调函数static size_t writeCallback(void* contents, size_t size, size_t nmemb, std::string* s);
};#endif // WIDGET_H
---------------------------------------------------------------------------------------------------
#include "widget.h"
#include <QVBoxLayout>
#include <QtWidgets>// 回调函数:接收 HTTP 响应数据
size_t Widget::writeCallback(void* contents, size_t size, size_t nmemb, std::string* s)
{size_t totalSize = size * nmemb;s->append(static_cast<char*>(contents), totalSize);return totalSize;
}Widget::Widget(QWidget *parent): QWidget(parent)
{// 设置窗口大小和标题this->setWindowTitle("访问百度测试");this->resize(400, 150);// 创建垂直布局QVBoxLayout *layout = new QVBoxLayout(this);// 创建按钮fetchButton = new QPushButton("访问百度", this);// 添加到布局layout->addWidget(fetchButton);// 连接按钮点击信号到槽函数connect(fetchButton, &QPushButton::clicked, this, &Widget::onFetchBaidu);
}Widget::~Widget()
{// 析构函数(libcurl 无需在这里清理)
}void Widget::onFetchBaidu()
{// 初始化 libcurlCURL *curl = curl_easy_init();if (!curl) {qDebug() << "libcurl 初始化失败!";return;}std::string response;// 设置请求选项curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // 跟随重定向curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); // 写入数据的回调curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); // 传递 response 字符串// 禁用 SSL 验证(解决 2025 年证书可能过期的问题)curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);// 执行请求CURLcode res = curl_easy_perform(curl);if (res != CURLE_OK) {qDebug() << "请求失败:" << curl_easy_strerror(res);} else {long statusCode;curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode);qDebug() << "请求成功!HTTP 状态码:" << statusCode;// 打印百度首页的 HTML 内容(UTF-8)qDebug() << "百度首页内容开始:";qDebug().noquote() << QString::fromUtf8(response.c_str());qDebug() << "内容结束。";}// 清理资源curl_easy_cleanup(curl);
}