QNetworkAccessManager读取outgoingData并将其保存在QIODevice中

发布于 2024-10-26 10:36:33 字数 998 浏览 6 评论 0原文

我正在尝试将所有传出的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

絕版丫頭 2024-11-02 10:36:33

qint64 QIODevice::peek(char * 数据,qint64 maxSize)

最多读取 maxSize 字节
设备写入数据,无副作用
(即,如果您在之后调用 read()
peek(),你会得到相同的数据)。
返回读取的字节数。如果
发生错误,例如当
试图查看打开的设备
WriteOnly模式,该函数返回
-1。

没有更多数据时返回0
可供阅读。

编辑

忘记peak(),在这种情况下它不好。您可以使用它,但您必须做很多工作才能完成您所要求的任务。而是阅读Tee is for Tubes,获取代码从那里开始并使用它。

链接由 peppe 提供,来自 http://irc.freenode.net< 上的 #qt irc 频道/a>.
我要感谢 peppethiago,他们非常友善地在 #qt 频道上与我讨论这个问题。

如果有一天您想要从 QNetworkAccessManager 窃取传入(而不是传出)数据,您将在 如何从 QWebPage 使用的 QNetworkReply 读取数据? 问题。

qint64 QIODevice::peek (char * data, qint64 maxSize)

Reads at most maxSize bytes from the
device into data, without side effects
(i.e., if you call read() after
peek(), you will get the same data).
Returns the number of bytes read. If
an error occurs, such as when
attempting to peek a device opened in
WriteOnly mode, this function returns
-1.

0 is returned when no more data is
available for reading.

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.

梦毁影碎の 2024-11-02 10:36:33

在这种特殊情况下,使用 pos() 和eek() 实际上不起作用。使用 peek() 代替的想法似乎要好得多。但举个例子会有帮助。因此,这里是如何在函数 createRequest() 中从给定 QIODevice 的传出数据获取数据缓冲区而不影响原始数据的示例。

if (outgoing != NULL)
{
    const qint64 delta = 100;

    qint64 length = delta;
    QByteArray array;

    while (true)
    {
        char *buffer = new char[length];
        qint64 count = outgoing->peek(buffer, length);

        if (count < length)
        {
            array = QByteArray(buffer, count);
            delete buffer;
            break;
        }

        length += delta;
        delete buffer;
    }
}

为了优化,您可以调整“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.

if (outgoing != NULL)
{
    const qint64 delta = 100;

    qint64 length = delta;
    QByteArray array;

    while (true)
    {
        char *buffer = new char[length];
        qint64 count = outgoing->peek(buffer, length);

        if (count < length)
        {
            array = QByteArray(buffer, count);
            delete buffer;
            break;
        }

        length += delta;
        delete buffer;
    }
}

For an optimization you may adjust the value of 'delta'.

双马尾 2024-11-02 10:36:33

使用 QIODevice::pos()。从中读取数据。然后使用 QIODevice::seek()

仅当设备是随机访问设备时,此功能才有效。但我认为它涵盖了大多数。

Save the IO device marker with QIODevice::pos(). Read data from it. Then restore the marker with QIODevice::seek().

This will only work if the device is a random access one. But I think it covers most of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文