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);
}
相关推荐
leo__5205 分钟前
C#与三菱PLC串口通信源码实现(基于MC协议)
开发语言·c#
二十雨辰37 分钟前
[python]-函数
开发语言·python
码农水水1 小时前
中国邮政Java面试被问:容器镜像的多阶段构建和优化
java·linux·开发语言·数据库·mysql·面试·php
福楠1 小时前
C++ STL | map、multimap
c语言·开发语言·数据结构·c++·算法
ytttr8731 小时前
地震数据频率波数域变换与去噪的MATLAB实现
开发语言·matlab
墨瑾轩1 小时前
C# PictureBox:5个技巧,从“普通控件“到“图像大师“的蜕变!
开发语言·c#·swift
墨瑾轩1 小时前
WinForm PictureBox控件:3个让图片“活“起来的骚操作,90%的开发者都踩过坑!
开发语言·c#
Ethernet_Comm2 小时前
从 C 转向 C++ 的过程
c语言·开发语言·c++
难得的我们2 小时前
C++与区块链智能合约
开发语言·c++·算法
jllllyuz2 小时前
基于MATLAB的D2D通信模式选择仿真
开发语言·网络·matlab