Reading HTTP headers

2019-04-17 08:59发布

问题:

I am trying to connect my application with a web service and here ,a user suggested to send custom headers back to my application.

I am using this code

void Coonnec::serviceRequestFinished(QNetworkReply *reply)
{
    QByteArray bytes = reply->readAll();

    if (reply->error() != QNetworkReply::NoError) {
        qDebug() << "Reply error: " + reply->errorString();
    }
    else
    {
        qDebug() << "Uploaded: " + QDateTime::currentDateTime().toString();
        qDebug() << reply->rawHeaderList();
    }
    reply->close();
    bytes.clear();
    reply->deleteLater();
}

from php i send this header

header('XAppRequest-Status: complete');

When running the application i can see that i get this header but i can't take the value of it cause

reply->rawHeader(bytes);

returns nothing.

How can i take the value 'complete'?

回答1:

I suggest to connect a slot to the void QNetworkReply::metaDataChanged () signal of your reply.

The Qt doc says

This signal is emitted whenever the metadata in this reply changes. metadata is any information that is not the content (data) itself, including the network headers. In the majority of cases, the metadata will be known fully by the time the first byte of data is received. However, it is possible to receive updates of headers or other metadata during the processing of the data.

I do use web-services/client with Qt and I noticed that some header's information are not available when I expected it to be ! I had to 'wait' for this signal to check the header content.