本页内容

QRestReply Class

QRestReply 是QNetworkReply 的便捷封装库。更多内容...

标题: #include <QRestReply>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
自: Qt 6.7

注意:该类中的所有函数均为可重入函数。

公共函数

QRestReply(QNetworkReply *reply)
QRestReply(QRestReply &&other)
~QRestReply()
QNetworkReply::NetworkError error() const
QString errorString() const
bool hasError() const
int httpStatus() const
bool isHttpStatusSuccess() const
bool isSuccess() const
QNetworkReply *networkReply() const
QByteArray readBody()
std::optional<QJsonDocument> readJson(QJsonParseError *error = nullptr)
QString readText()
QRestReply &operator=(QRestReply &&other)
QDebug operator<<(QDebug debug, const QRestReply &reply)

详细描述

QRestReply 封装了QNetworkReply ,并为数据和状态处理提供了便捷的方法。这些方法为典型的 RESTful 客户端应用程序提供了便利。

QRestReply 不会拥有被封装的QNetworkReply ,其生命周期和所有权由QNetworkAccessManager 文档中定义。

QRestReply 对象不可复制,但可移动。

另请参阅 QRestAccessManager 、QNetworkReply 、QNetworkAccessManager 以及QNetworkAccessManager::setAutoDeleteReplies()。

成员函数文档

[explicit] QRestReply::QRestReply(QNetworkReply *reply)

创建一个 QRestReply 对象,并将封装的QNetworkReply 初始化为reply 。

[noexcept] QRestReply::QRestReply(QRestReply &&other)

从other 处通过移动构造方式生成回复。

注意: 被移动的对象 other 处于部分形成状态,在此状态下,唯一有效的操作是销毁和赋值。

[noexcept] QRestReply::~QRestReply()

销毁此QRestReply 对象。

QNetworkReply::NetworkError QRestReply::error() const

返回最后一个错误(如有)。这些错误包括网络和协议错误等,但不包括服务器成功返回 HTTP 状态码的情况。

另请参阅 httpStatus()、isSuccess()、hasError() 以及errorString()。

QString QRestReply::errorString() const

返回上次网络错误的通俗易懂的描述。

另请参阅 httpStatus()、isSuccess()、hasError() 以及error()。

bool QRestReply::hasError() const

返回是否发生了错误。这包括网络和协议错误等,但不包括服务器成功返回HTTP错误状态码的情况(例如500 Internal Server Error )。请使用httpStatus() 或isHttpStatusSuccess() 来获取HTTP状态信息。

另请参阅 httpStatus()、isSuccess()、error() 和errorString()。

int QRestReply::httpStatus() const

返回服务器响应中收到的 HTTP 状态码。如果无法获取(即尚未收到状态行),则返回值为0。

注意: HTTP状态是 根据接收到的 HTTP 响应报告的。在接收状态后可能会发生error() 错误,例如在接收长响应时因网络断开而导致的错误。这些潜在的后续错误不会体现在报告的 HTTP 状态中。

另请参阅 isSuccess()、hasError() 和error()。

bool QRestReply::isHttpStatusSuccess() const

返回 HTTP 状态码是否在 200..299 之间。

另请参阅 isSuccess()、httpStatus()、hasError() 以及error()。

bool QRestReply::isSuccess() const

返回 HTTP 状态码是否在 200..299 范围内,且在接收响应过程中未发生其他错误(例如,在接收正文数据时突然断开连接)。此函数提供了一种便捷的方式来检查响应是否被视为成功。

另请参阅 httpStatus()、hasError()、以及error()。

QNetworkReply *QRestReply::networkReply() const

返回指向该对象所封装的底层QNetworkReply 的指针。

QByteArray QRestReply::readBody()

将接收到的数据作为QByteArray 返回。

调用此函数会消耗迄今为止接收到的数据,此后任何获取响应数据的调用都将返回空结果,直到收到更多数据为止。

另请参阅 readJson()、readText()、QNetworkReply::bytesAvailable() 以及QNetworkReply::readyRead()。

std::optional<QJsonDocument> QRestReply::readJson(QJsonParseError *error = nullptr)

将接收到的数据作为QJsonDocument 返回。

返回值被封装在std::optional 中。如果从接收到的数据进行转换失败(数据为空或发生JSON解析错误),则返回std::nullopt ,同时error 中会填入详细信息。

调用此函数会消耗接收到的数据,此后任何获取响应数据的调用都将返回空值。

如果响应尚未完成,该函数将返回 `std::nullopt ` 且不会消耗任何数据。如果传入了 `error `,它将被赋值给 `QJsonParseError::NoError `,以将此情况与实际错误区分开来。

另请参阅 readBody() 和readText()。

QString QRestReply::readText()

将接收到的数据作为QString 返回。

将接收到的数据解码为QString (UTF-16)。如果可用,解码过程将使用Content-Type头中的 charset参数来确定源编码。如果编码信息不可用或QStringConverter 不支持该编码,则默认使用UTF-8。

调用此函数会消耗迄今为止接收到的数据。若无新数据可用、QStringConverter 不支持该解码方式,或解码过程中出现错误(例如遇到无效字符),则返回默认构造的值。

另请参阅 readJson()、readBody() 和QNetworkReply::readyRead()。

[noexcept] QRestReply &QRestReply::operator=(QRestReply &&other)

将other 移入并赋值,并返回对此回复的引用。

注意: 被移动的对象 other 将处于部分构筑状态,在此状态下,唯一有效的操作是销毁和赋值。

相关的非成员

QDebug operator<<(QDebug debug, const QRestReply &reply)

将reply 写入debug 对象中,以便进行调试。

另请参阅 “调试技术”。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.