View Full Version : Is it possible to get rawHeaders from QNetworkReply without waiting to finished?
Alir3z4
12th February 2012, 08:28
Hi!
the title describes everything! :|
for more details:
i want o download a file, but before to download it, i want to get the file size by the Url, but i couldn't get it till QNetworkAccessManager or QNetworkReply signals[finished, readyRead] emited.
Can i retrieve the headers before to those signals emit ?
ChrisW67
12th February 2012, 23:26
Assuming you are talking about HTTP here are some options:
Send a head() request to get the headers without the body.
Connect something to the QNetworkReply::metaDataChanged() signal and look at the headers at that point.
Alir3z4
14th February 2012, 21:27
Simply i give-up so far done that in separate function to handle something like that, similar to startRequest() function from DownloadManager example.
ChrisW67
14th February 2012, 22:07
:confused: I could not make any sense of your last post. You've given up? Did you try anything I suggested?
Here is a working example:
#include <QtCore>
#include <QtNetwork>
#include <QDebug>
class Fetcher: public QObject
{
Q_OBJECT
public:
Fetcher(QObject *p = 0): QObject(p)
{
QTimer::singleShot(0, this, SLOT(start()));
}
public slots:
void start() {
fetched = 0;
size = 0;
QNetworkRequest req(QUrl("http://qt.nokia.com/files/pdf/faster-more-cost-effective-development-using-the-qt-cross-platform-application-ui-framework"));
reply = nam.get(req);
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}
void replyMetaDataChanged() {
qDebug() << "Metadata changed";
qDebug() << "Content-Length:" << reply->header(QNetworkRequest::ContentLengthHeader);
size = reply->header(QNetworkRequest::ContentLengthHeader).toLon gLong();
}
void slotReadyRead() {
QByteArray ba = reply->readAll();
fetched += ba.size();
qDebug() << "Read" << fetched << "of" << size;
}
void replyFinished() {
qDebug() << "Finished";
}
private:
QNetworkAccessManager nam;
QNetworkReply *reply;
qint64 fetched;
qint64 size;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Fetcher f;
return app.exec();
}
#include "main.moc"
You will strike problems with the Content-Length header as it is not mandatory.
Alir3z4
14th February 2012, 22:13
Yes, But after that i figure it out
before to download it, i want to get the file size by the Url, but i couldn't get it till QNetworkAccessManager or QNetworkReply signals[finished, readyRead] emited.
is somehow or totally stupid, i could simply just move the logic of retrieving the file size after the those signals emitted then do whatever on it.
ChrisW67
14th February 2012, 23:01
Take a look at my last post (I edited it while you were replying). If you do head() rather than get() you only get the headers - the example actually fetches the file but sees the headers before it sees the data.
Alir3z4
14th February 2012, 23:17
Wow, thank you! :x
now i got it.
This way you provide, it's make code more clear and logical !
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.