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);
}
相关推荐
多加点辣也没关系5 分钟前
JavaScript|第30章:事件机制
开发语言·javascript
小大宇6 分钟前
python milvus 案例
开发语言·python·milvus
Gauss松鼠会17 分钟前
【GaussDB】GaussDB锁阻塞源头查询
java·开发语言·前端·数据库·算法·gaussdb·经验总结
mingo_敏34 分钟前
DeepAgents : 后端(Backends)
java·开发语言
mabing9931 小时前
Qt QMessageBox、QDialogButtonBox中英文翻译动态切换
开发语言·qt
Sagittarius_A*1 小时前
【RCELABS】Level 17~18 —— PHP命令执行函数与环境变量注入
开发语言·安全·web安全·靶场·php·rce
冻柠檬飞冰走茶1 小时前
PTA基础编程题目集 7-19 支票面额(C语言实现)
c语言·开发语言·数据结构·算法
破碎的南瓜1 小时前
靶场中使用到的php函数以及每种漏洞的防御方式
开发语言·php
秋田君1 小时前
QT_QColorDialog颜色对话框
java·数据库·qt
谁看我谁 狼家二丫2 小时前
局域网文件共享实战:从“账户被禁用”到成功互传文件
开发语言·php