0、说明

QNetworkReply对象包含了Manager发送的请求头和返回的数据。

它继承自QIODevice,所以可以用各种read获取其中返回的数据:

QByteArray data = reply->readAll();

QNetworkReply是顺序访问的QIODevice,这意味着一旦数据从中读取出来之后,它就不再为Device保存了。因此保存数据的工作必须要由应用程序自己来实现。每当有新的数据从网络中传来时,readyRead()信号就会被发送。

此外,数据接收时downloadProgress()信号也会被发送,只是其中包含的bytes数并不代表实际接收的bytes,因为可能存在着数据转换(如解压缩和撤销协议)。

它也会发送uploadProgress()信号,指示上传这些内容的进度。

注意

不要在信号errorOccurred()和finished()的槽中删除Object,而应该使用deleteLater()。

1、模块和加载项

Header: #include <QNetworkReply>
qmake: QT += network
Since: Qt 4.4
Inherits: QIODevice

2、构造

3、静态字段

类型

字段

说明

enum NetworkError

标识在处理Request期间可能发生的所有错误。

常见的是QNetworkReply::NoError,标识没有错误。

typedef RawHeaderPair 一个QPair < QByteArray , QByteArray >,第一个QByteArray是header name,第二个QByteArray是header

NetworkError

Constant Value Description
QNetworkReply::ConnectionRefusedError 1 the remote server refused the connection (the server is not accepting requests)
QNetworkReply::RemoteHostClosedError 2 the remote server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::HostNotFoundError 3 the remote host name was not found (invalid hostname)
QNetworkReply::TimeoutError 4 the connection to the remote server timed out
QNetworkReply::OperationCanceledError 5 the operation was canceled via calls to abort() or close() before it was finished.
QNetworkReply::SslHandshakeFailedError 6 the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors() signal should have been emitted.
QNetworkReply::TemporaryNetworkFailureError 7 the connection was broken due to disconnection from the network, however the system has initiated roaming to another access point. The request should be resubmitted and will be processed as soon as the connection is re-established.
QNetworkReply::NetworkSessionFailedError 8 the connection was broken due to disconnection from the network or failure to start the network.
QNetworkReply::BackgroundRequestNotAllowedError 9 the background request is not currently allowed due to platform policy.
QNetworkReply::TooManyRedirectsError 10 while following redirects, the maximum limit was reached. The limit is by default set to 50 or as set by QNetworkRequest::setMaxRedirectsAllowed(). (This value was introduced in 5.6.)
QNetworkReply::InsecureRedirectError 11 while following redirects, the network access API detected a redirect from a encrypted protocol (https) to an unencrypted one (http). (This value was introduced in 5.6.)
QNetworkReply::ProxyConnectionRefusedError 101 the connection to the proxy server was refused (the proxy server is not accepting requests)
QNetworkReply::ProxyConnectionClosedError 102 the proxy server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::ProxyNotFoundError 103 the proxy host name was not found (invalid proxy hostname)
QNetworkReply::ProxyTimeoutError 104 the connection to the proxy timed out or the proxy did not reply in time to the request sent
QNetworkReply::ProxyAuthenticationRequiredError 105 the proxy requires authentication in order to honour the request but did not accept any credentials offered (if any)
QNetworkReply::ContentAccessDenied 201 the access to the remote content was denied (similar to HTTP error 403)
QNetworkReply::ContentOperationNotPermittedError 202 the operation requested on the remote content is not permitted
QNetworkReply::ContentNotFoundError 203 the remote content was not found at the server (similar to HTTP error 404)
QNetworkReply::AuthenticationRequiredError 204 the remote server requires authentication to serve the content but the credentials provided were not accepted (if any)
QNetworkReply::ContentReSendError 205 the request needed to be sent again, but this failed for example because the upload data could not be read a second time.
QNetworkReply::ContentConflictError 206 the request could not be completed due to a conflict with the current state of the resource.
QNetworkReply::ContentGoneError 207 the requested resource is no longer available at the server.
QNetworkReply::InternalServerError 401 the server encountered an unexpected condition which prevented it from fulfilling the request.
QNetworkReply::OperationNotImplementedError 402 the server does not support the functionality required to fulfill the request.
QNetworkReply::ServiceUnavailableError 403 the server is unable to handle the request at this time.
QNetworkReply::ProtocolUnknownError 301 the Network Access API cannot honor the request because the protocol is not known
QNetworkReply::ProtocolInvalidOperationError 302 the requested operation is invalid for this protocol
QNetworkReply::UnknownNetworkError 99 an unknown network-related error was detected
QNetworkReply::UnknownProxyError 199 an unknown proxy-related error was detected
QNetworkReply::UnknownContentError 299 an unknown error related to the remote content was detected
QNetworkReply::ProtocolFailure 399 a breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.)
QNetworkReply::UnknownServerError 499 an unknown error related to the server response was detected

4、实例方法

返回值类型

方法

说明

QVariant attribute(QNetworkRequest::Attribute code)  
QNetworkReply::NetworkError error()  
bool hasRawHeader(QByteArray headerName)  
QVariant header(QNetworkRequest::KnownHeaders header)  
void ignoreSslErrors(QList<QSslError> errors)  
bool isFinished()  
bool isRunning()  
QNetworkAccessManager * manager()  
QNetworkAccessManager::Operation operation()  
QByteArray rawHeader(QByteArray headerName)  
QList<QByteArray> rawHeaderList()  
QList<QNetworkReply::RawHeaderPair> rawHeaderPairs()  
qint64 readBufferSize()  
QNetworkRequest request()  
virtual void setReadBufferSize(qint64 size)  
void setSslConfiguration(QSslConfiguration config)  
QSslConfiguration sslConfiguration()  
QUrl url()  

5、信号

信号

说明

downloadProgress(qint64 bytesReceived, qint64 bytesTotal)  
encrypted()  
errorOccurred(QNetworkReply::NetworkError code)  
finished()  
metaDataChanged()  
preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator)  
redirectAllowed()  
redirected(QUrl url)  
sslErrors(QList<QSslError> errors)  
uploadProgress(qint64 bytesSent, qint64 bytesTotal)  

Qt:QNetworkReply的更多相关文章

  1. Qt:禁止qDebug的输出

    Qt:禁止qDebug的输出 在工程的.pro文件里加上以下编译批令即可: DEFINES += QT_NO_DEBUG_OUTPUT

  2. Qt:使用自定义的字体

    Qt:使用自定义的字体 1. 下载字体文件 2. 加载字体文件 3. 使用字体   QFontDatabase::addApplicationFont("XENOTRON.TTF" ...

  3. QT 通过QNetworkReply *获取对应请求的URL地址

    [1]QT 通过QNetworkReply *获取对应请求的URL地址 reply->url().toString(); Good Good Study, Day Day Up. 顺序 选择 循 ...

  4. Qt:QJsonObject

    0.说明 QJsonObject在逻辑上就是一个Map或Dict!记住这一点对理解它的方法.说明很有帮助. QJsonObject类封装了JSON Object. JSON Object是一个Key- ...

  5. Qt:QJsonValue

    0.说明 QJsonValue类用于操作JSON中的各种数据. JSON是用于存储结构化数据的格式,JSON中的数据可以是六种类型: 基本类型 存储类型 bool QJsonValue::Bool d ...

  6. Qt:QJsonArray

    0.说明 QJsonArray中存储了一系列的QJsonValue.可以向其中插入.删除QJsonValue. 一个QJsonArray可以与QVariantList互相转换.可以通过size()访问 ...

  7. Qt:QUrl构造时的qrc前缀

    参考(按对我帮助从大到小排列): Qt内的各种路径(让人迷惑) - 鬼谷子com - 博客园 qt webengineview 加载本地资源方式 - beautifulday - 博客园 (17条消息 ...

  8. QT:多线程HTTP下载文件

    这里的线程是指下载的通道(和操作系统中的线程不一样),一个线程就是一个文件的下载通道,多线程也就是同时开起好几个下载通道.当服务器提供下载服务时,使用下载者是共享带宽的,在优先级相同的情况下,总服务器 ...

  9. Qt:QNetworkAccessManager

    0.说明 QNetworkAccessManager允许应用发送Request并接受回应. 网络访问API是围绕一个QNetworkAccessManager对象构建的,该对象保留了所有它发送的请求的 ...

随机推荐

  1. 学习JAVAWEB第一天

    第一天:单元测试(junit)黑盒测试:不需要写代码,给输入值,看程序能否给出期望值白盒测试:需要写代码,关注程序的具体执行流程junit使用步骤:步骤1:定义一个测试类建议类名,被测试类名后面加一个 ...

  2. Python反爬:利用js逆向和woff文件爬取猫眼电影评分信息

    首先:看看运行结果效果如何! 1. 实现思路 小编基本实现思路如下: 利用js逆向模拟请求得到电影评分的页面(就是猫眼电影的评分信息并不是我们上述看到的那个页面上,应该它的实现是在一个页面上插入另外一 ...

  3. 被mybatis一级缓存坑了

    目录 背景 场景 原因 解法 参考 背景 项目中出现了这样一个问题,就是select出来的数据和数据库里的数据不一样,就非常的奇怪,发现原来是mybatis的缓存导致的,经过查询资料发现这是mybat ...

  4. Lesson5——Pandas Panel三维数据结构

    pandas目录 1 简介 自 Pandas 0.25 版本后, Panel 结构已经被废弃. pd.__version__ #查看pandas版本 #'1.2.4' #或者 pd.show_vers ...

  5. 使用gdi+实时绘制picturebox(画个叉)

    private void DrawReticle(System.Drawing.Point pt, int size)//画一个透明的前景图片上画十字 { Bitmap bmp = new Bitma ...

  6. Go 学习路线(2022)

    原文链接: Go 学习路线(2022) Go 语言的发展越来越好了,很多大厂使用 Go 作为主要开发语言,也有很多人开始学习 Go,准备转 Go 开发. 那么,怎么学呢? 我发现,在互联网时代,学习的 ...

  7. Redis设计与实现

    简述Redis设计与实现 Redis是一个高性能的key-value的非关系型数据库,Redis是运行在内存中的一种数据库,但是它也可以持久化到磁盘中,Redis的实现有着更为复杂的数据结构并且提供对 ...

  8. Collection和Map集合

    前言 集合的长度是可变的.集合存储的都是对象.而且对象的类型可以不一致. Collection集合和Map集合是所有集合的父接口. Collection接口定义 体系结构的根接口,代表一组对象,有两个 ...

  9. elasticsearch搜索引擎的常用方法

    1.term和termsterm和terms等查询,不会对查询对字段进行分词处理,适合于date.num.id等确切数据进行搜索 如果需要查询keywords,则查询等keywords必须是查询字段中 ...

  10. Pytorch技法:继承Subset类完成自定义数据拆分

    我们在<torch.utils.data.DataLoader与迭代器转换>中介绍了如何使用Pytorch内置的数据集进行论文实现,如torchvision.datasets.下面是加载内 ...