Win10上Qt使用Libcurl库

第1章 下载libcurl

下载链接

第2章 curl集成到Qt中

  1. 将已经编译好的libcurl目录下的lib和include目录拷贝到项目源码目录中。

|----------------------------------------------------------------------------|
| |

|----------------------------------------------------------------------------|
| |

  1. 将libcurl目录下的bin目录下的【libcurl-x64.dll】动态库拷贝到源码目录下可执行文件【testdemo3.exe】位置。

第3章 配置qmake

cpp 复制代码
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += 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.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    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章 访问百度测试

cpp 复制代码
#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_OBJECT

public:
    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

cpp 复制代码
#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()
{
    // 初始化 libcurl
    CURL *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);
}
相关推荐
yiangL_11 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
geovindu13 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk816314 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾15 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
听情歌落俗16 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力16 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
ShineWinsu16 小时前
对于Qt:QFontDialog的解析
c++·qt·面试·gui·字体·客户端·qfontdialog
落羽的落羽18 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
小飞在努力19 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
炸薯条!19 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python