QNetworkAccessManager读取outgoingData并将其保存在QIODevice中
我正在尝试将所有传出的 POST 数据保存在 QtWebKit 中。
我使用重写 QNetworkReply *QNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice outgoingData) 方法并读取包含传出 POST 数据的传出数据来执行此操作。
问题是读取后,数据在 QIODevice 中变得不可用。
如何保存传出(PUT、POST)数据并使其可用于将来的内部 Qt 操作?
如果我需要使用另一种方法来保存 PUT/POST 数据 - 请告诉我。
代码示例:
QNetworkReply *MyNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
{
QByteArray bArray = outgoingData->readAll();
// save bArray (that contains POST outgoing data) somewhere
// do other things, and outgoingData now has no data anymore, as it was already read to bArray
}
我已经尝试过
QByteArray bArray = outgoingData->readAll();
outgoingData->write(bArray);
qDebug() << bArray;
但在这种情况下我收到“QIODevice::write: ReadOnly device”消息。
如何在 Qt 中保存传出的 POST/PUT 数据?
谢谢。
I'm trying to save all outgoing POST data in QtWebKit.
I do it using overriding QNetworkReply *QNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice outgoingData) method and reading an outgoingData that contains outgoing POST data.
The problem is that after reading it, the data become not available in the QIODevice.
How to save an outgoing (PUT, POST) data and keep it available for the future internal Qt operations?
If I need to use another approach to save PUT/POST data - please, let me know.
Code example:
QNetworkReply *MyNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
{
QByteArray bArray = outgoingData->readAll();
// save bArray (that contains POST outgoing data) somewhere
// do other things, and outgoingData now has no data anymore, as it was already read to bArray
}
I have tried
QByteArray bArray = outgoingData->readAll();
outgoingData->write(bArray);
qDebug() << bArray;
But in this case I get "QIODevice::write: ReadOnly device" message.
How to save the outgoing POST/PUT data in Qt?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
qint64 QIODevice::peek(char * 数据,qint64 maxSize)
编辑
忘记peak(),在这种情况下它不好。您可以使用它,但您必须做很多工作才能完成您所要求的任务。而是阅读Tee is for Tubes,获取代码从那里开始并使用它。
链接由 peppe 提供,来自 http://irc.freenode.net< 上的 #qt irc 频道/a>.
我要感谢 peppe 和 thiago,他们非常友善地在 #qt 频道上与我讨论这个问题。
如果有一天您想要从 QNetworkAccessManager 窃取传入(而不是传出)数据,您将在 如何从 QWebPage 使用的 QNetworkReply 读取数据? 问题。
qint64 QIODevice::peek (char * data, qint64 maxSize)
EDIT
Forget about peak(), it's not good in this situation. You could use it but you would have to do much work to accomplish what you ask for. Instead read Tee is for Tubes, grab code from there and use it.
Link by courtesy of peppe from #qt irc channel on http://irc.freenode.net.
I'd like to thank peppe and thiago who were so kind to discuss this problem on #qt channel with me.
In case one day you want to steal incoming (as opposed to outgoing) data from QNetworkAccessManager you'll find answer and code in How to read data from QNetworkReply being used by QWebPage? question.
在这种特殊情况下,使用 pos() 和eek() 实际上不起作用。使用 peek() 代替的想法似乎要好得多。但举个例子会有帮助。因此,这里是如何在函数 createRequest() 中从给定 QIODevice 的传出数据获取数据缓冲区而不影响原始数据的示例。
为了优化,您可以调整“delta”的值。
Using pos() and seek() does actually not work in that special case. The idea of using peek() instead seems to be much better. But an example would be helpful. So, here an example of how to get data buffer from given QIODevice's outgoing data in function createRequest() without affecting original data.
For an optimization you may adjust the value of 'delta'.
使用
QIODevice::pos()
。从中读取数据。然后使用
QIODevice::seek()
。仅当设备是随机访问设备时,此功能才有效。但我认为它涵盖了大多数。
Save the IO device marker with
QIODevice::pos()
. Read data from it. Then restore the marker withQIODevice::seek()
.This will only work if the device is a random access one. But I think it covers most of them.