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 是网络编程 API 的一部分。
注意:该类中的所有函数都是可重入的。
公共函数
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 构建回复。
注: moved-from 对象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 ,以区别于实际错误。
QString QRestReply::readText()
以QString 的形式返回接收到的数据。
接收到的数据将解码为QString (UTF-16)。如果可用,解码会使用Content-Type标头的charset参数来确定源编码。如果编码信息不可用或QStringConverter 不支持,则默认使用 UTF-8。
调用此函数将消耗迄今收到的数据。如果没有新数据,或QStringConverter 不支持解码,或解码出现错误(如无效字符),则返回默认构造值。
另请参阅 readJson()、readBody() 和QNetworkReply::readyRead()。
[noexcept]
QRestReply &QRestReply::operator=(QRestReply &&other)
Move-assignsother 并返回对该回复的引用。
注: moved-from 对象other 处于部分形成状态,在这种状态下,唯一有效的操作是销毁和赋新值。
© 2025 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.