Results 1 to 7 of 7

Thread: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

  1. #1
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    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 ?
    Last edited by Alir3z4; 12th February 2012 at 08:40.
    ...یه مرد هیچوقت زمین نمیخوره

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    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.

  3. #3
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    Simply i give-up so far done that in separate function to handle something like that, similar to startRequest() function from DownloadManager example.
    Last edited by Alir3z4; 14th February 2012 at 22:14.
    ...یه مرد هیچوقت زمین نمیخوره

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    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:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtNetwork>
    3. #include <QDebug>
    4.  
    5. class Fetcher: public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. Fetcher(QObject *p = 0): QObject(p)
    10. {
    11. QTimer::singleShot(0, this, SLOT(start()));
    12. }
    13.  
    14. public slots:
    15. void start() {
    16. fetched = 0;
    17. size = 0;
    18. QNetworkRequest req(QUrl("http://qt.nokia.com/files/pdf/faster-more-cost-effective-development-using-the-qt-cross-platform-application-ui-framework"));
    19. reply = nam.get(req);
    20.  
    21. connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
    22. connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
    23. connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
    24. }
    25.  
    26. void replyMetaDataChanged() {
    27. qDebug() << "Metadata changed";
    28. qDebug() << "Content-Length:" << reply->header(QNetworkRequest::ContentLengthHeader);
    29. size = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
    30. }
    31.  
    32. void slotReadyRead() {
    33. QByteArray ba = reply->readAll();
    34. fetched += ba.size();
    35. qDebug() << "Read" << fetched << "of" << size;
    36.  
    37. }
    38.  
    39. void replyFinished() {
    40. qDebug() << "Finished";
    41. }
    42.  
    43. private:
    44. QNetworkAccessManager nam;
    45. QNetworkReply *reply;
    46. qint64 fetched;
    47. qint64 size;
    48. };
    49.  
    50. int main(int argc, char *argv[])
    51. {
    52. QCoreApplication app(argc, argv);
    53.  
    54. Fetcher f;
    55. return app.exec();
    56. }
    57. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    You will strike problems with the Content-Length header as it is not mandatory.
    Last edited by ChrisW67; 14th February 2012 at 22:49.

  5. The following user says thank you to ChrisW67 for this useful post:

    Alir3z4 (14th February 2012)

  6. #5
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    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.
    ...یه مرد هیچوقت زمین نمیخوره

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    Alir3z4 (14th February 2012)

  9. #7
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Is it possible to get rawHeaders from QNetworkReply without waiting to finished?

    Wow, thank you! :x
    now i got it.
    This way you provide, it's make code more clear and logical !
    ...یه مرد هیچوقت زمین نمیخوره

Similar Threads

  1. Waiting for a download event
    By merajc in forum Newbie
    Replies: 6
    Last Post: 27th January 2011, 07:31
  2. Waiting WebKit
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 13th May 2009, 03:40
  3. Waiting for multiple threads
    By Bebee in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2008, 17:21
  4. Waiting on a thread?
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 16:13
  5. Waiting for something
    By JonathanForQT4 in forum Newbie
    Replies: 20
    Last Post: 2nd May 2007, 17:35

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.