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);
}
相关推荐
小周同学:13 分钟前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
sqmeeting1 小时前
QT6 如何在Linux Wayland 桌面系统抓屏和分享屏幕
linux·qt
teeeeeeemo1 小时前
跨域及解决方案
开发语言·前端·javascript·笔记
极客BIM工作室2 小时前
谈谈《More Effective C++》的条款30:代理类
java·开发语言·c++
躲在云朵里`2 小时前
常用Linux指令:Java/MySQL/Tomcat/Redis/Nginx运维指南
开发语言·python
liulanba3 小时前
NAT 和 PNAT
开发语言·php
hqwest4 小时前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
檀越剑指大厂4 小时前
【开发语言】Groovy语言:Java生态中的动态力量
java·开发语言
stbomei4 小时前
C 语言判断一个数是否是素数的三种方法文章提纲
c语言·开发语言·算法