如何从 QWebPage 使用的 QNetworkReply 读取数据?
我使用 QWebPage
下载网页以及其所有资源。同时,我想在此过程中获取 Qt 下载的原始数据。通过读取 QNetworkReply
中的数据来执行此操作href="http://doc.qt.nokia.com/latest/qnetworkaccessmanager.html#finished" rel="nofollow">void QNetworkAccessManager::finished(QNetworkReply * 回复)
signal 不是一个好的解决方案,因为数据可能已经被 QWebPage 本身读取。这是因为
QNetworkReply 是一个顺序访问 QIODevice,表示一旦数据 从对象中读取,它不再 由设备保存。
根据QNetworkReply
的详细描述。
但是,QWebPage
可以配置为使用自定义QNetworkAccessManager
与覆盖
createRequest
方法
QNetworkReply * QNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 )
我认为正确的解决方案是为 QNetworkReply
创建代理并在 createRequest
中返回它方法。该代理应该允许从回复中读取数据,就像原始 QNetworkReply 的情况一样(以便 QWebPage 可以从中读取数据),但同时该代理应该允许用于在 QWebPage
读取数据后由其他对象读取数据。换句话说,我们需要用于 QNetworkReply
的 tee IODevice
基类。
这个代理怎么写呢?
I use QWebPage
to download a webpage as well as all its resources. At the same time I'd like to get hold on raw data being downloaded by Qt during this process. Doing this by reading data from QNetworkReply
in void QNetworkAccessManager::finished(QNetworkReply * reply)
signal is not a good solution as data could have been already read by QWebPage
itself. This is because
QNetworkReply is a sequential-access
QIODevice, which means that once data
is read from the object, it no longer
kept by the device.
according to detailed description of QNetworkReply
.
However QWebPage
can be configured to use custom QNetworkAccessManager
with overriden createRequest
method
QNetworkReply * QNetworkAccessManager::createRequest ( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 )
I think the right solution would be to create a proxy for QNetworkReply
and return it in the createRequest
method. This proxy should allow for reading data from reply as is the case with the original QNetworkReply
(so that QWebPage
could read data from it) but at the same time this proxy should allow for reading data by other objects after it have been read by QWebPage
. In other words we need tee for QNetworkReply
's IODevice
base class.
How to write this proxy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来有人已经想要同样的东西并编写了 代理对于 QNetworkReply。
It looks like someone has already wanted the same and wrote a proxy for the QNetworkReply.