简体   繁体   中英

Save QNetworkReply

I would like to be able to save the QNetworkReply to a QString/QByteArray. In the examples I've seen they always saves the stream to another file.

At the moment my code looks something like this, where I get a string from the host and all I want to do is to parse it to look for the specified error code.

if(_reply->error() == QNetworkReply::UnknownContentError) {

    qDebug() << _reply->readAll(); // prints out the xml message

    QString test = QString(_reply->readAll());
    qDebug() << test; // ""

    QByteArray test2 = QByteArray(_reply->readAll());
    qDebug() << test2; // ""

    QRegExp rxlen("(<code>)(.*(?=</code>))");
    rxlen.setMinimal(true);

    int pos = rxlen.indexIn(test); // pos == -1

    if(pos > -1) {
        qDebug() << rxlen.cap(2); // never hit
    }

}

The message is pretty small and looks something like this:

<?xml version="1.0" encoding="utf-8"?>
   <error>
      <code>string-value</code>
      <message>string-value</message>
   </error>

So how can I load this small stream into memory, or just look for the error code?

QNetworkReply inherits from QIODevice which is a stream. After you have read something from a stream it's not there anymore. After your debug line (one with // prints out the xml message comment) there's nothing to read anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM