Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkReply readAll() gets empty data on finished()
Qt 6.12 LTS is out! See what's new in the release blog

QNetworkReply readAll() gets empty data on finished()

Scheduled Pinned Locked Moved General and Desktop
25 Posts 7 Posters 27.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #12

    Sure its the default template for Qt Widgets Aplication with Dialog as base class. Here's the header
    @
    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    public slots:
    void onFinished(QNetworkReply *reply);

    private:
    Ui::Dialog *ui;
    QNetworkAccessManager m_qnam;
    };

    #endif // DIALOG_H
    @

    157

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #13

      And dialog.cpp
      @
      #include "dialog.h"
      #include "ui_dialog.h"

      #include <QNetworkRequest>

      Dialog::Dialog(QWidget parent) :
      QDialog(parent),
      ui(new Ui::Dialog), m_qnam()
      {
      ui->setupUi(this);
      connect(&m_qnam,SIGNAL(finished(QNetworkReply
      )),
      this,SLOT(onFinished(QNetworkReply*)));
      QUrl url = QString::fromLocal8Bit("http://www.qt-project.org");
      QNetworkRequest request(url);
      m_qnam.get(request);
      }

      Dialog::~Dialog()
      {
      delete ui;
      }

      void Dialog::onFinished(QNetworkReply* reply)
      {
      QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
      qDebug() << redirectionTarget;
      if (!redirectionTarget.isNull()) {
      QUrl newUrl = QUrl(redirectionTarget.toUrl());
      QNetworkRequest request(newUrl);
      m_qnam.get(request);
      } else {
      qDebug() << reply->readAll();
      }
      reply->deleteLater();
      }
      @

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Almer_Cz
        wrote on last edited by
        #14

        Result:

        @'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\bin\icuin53.dll', Binary was not built with debug information.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\bin\icuuc53.dll', Binary was not built with debug information.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\bin\icudt53.dll', Binary was not built with debug information.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\bin\Qt5Widgetsd.dll', Symbols loaded.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\bin\Qt5Guid.dll', Symbols loaded.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\bin\Qt5Networkd.dll', Symbols loaded.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\plugins\platforms\qwindowsd.dll', Symbols loaded.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\plugins\bearer\qgenericbearerd.dll', Symbols loaded.
        'Sample.exe': Loaded 'D:\Dev\qt-prebuild\5.4\msvc2010_opengl\plugins\bearer\qnativewifibearerd.dll', Symbols loaded.
        QVariant(QUrl, QUrl("http://qt-project.org/") )
        The thread 'Win32 Thread' (0x1148) has exited with code 0 (0x0).
        QVariant(Invalid)
        @

        So, first time it is redirecting - ok, second time is requested url and no output....

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #15

          Strange. Which Qt Version?
          Anyway, "here's":https://drive.google.com/file/d/0B3rkZH6q1E7oV2MwbkUzTVRrQlE/view?usp=sharing the complete project
          Download and check if it works as it is.

          157

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Almer_Cz
            wrote on last edited by
            #16

            As original post (and tags) says:

            Qt 5.1.1 (msvc 2010 version without OGL) and Qt 5.4 (msvc 2010 version with OGL). Downloaded from online installer...

            Edit: ur code has same result..

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #17

              That's weird. It works perfectly on my system, Ubuntu 14.04. Try removing reply->deleteLater() from onFinished(QNetworkReply* reply) slot.

              157

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Almer_Cz
                wrote on last edited by
                #18

                Surely. I am not new to Qt even in programming.

                I tried even my own compiled versions and same result. I have also tried make it synchronous (after calling get, i entered into event loop and woke up when finished is emit).

                The only thing which works is catching readyRead signal and store chunks.

                For my perspective, something is reading that buffer before i get it on finish slot. Or closes it or idk...

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #19

                  But there's nothing in the code that I sent which would cause some abrupt buffer reading. I'm now running out if ideas.
                  Surely someone other has a better explanation for this behaviour.

                  157

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    ckakman
                    wrote on last edited by
                    #20

                    Is it possible, somehow, that the function in which the request is sent is called more than once? If this is the case, than when the slot is called, let's say, for the first QNetworkReply object, you access the second or third instance which has no data yet.

                    Even if this is not the case, it is not a good idea to keep a member variable to the QNetworkReply object. Connecting the slot to the QNAM::finished(QNetworkReply*) signal is better because you know for sure from which QNetworkReply object to readAll().

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Almer_Cz
                      wrote on last edited by
                      #21

                      Hi,

                      didn't see you replied here. I am unable to fix it, even when storing link to QNetworkReply - and check whenever sender() is this reply.

                      It seems that buffer is somehow read in inner slot, before emitting finish(). and it doesnt matter if it is emitted by QNR or QNAM.

                      It seems not only me has same problem: "http://qt-project.org/forums/viewthread/51848/":http://qt-project.org/forums/viewthread/51848/

                      Edit: it seems to have something to do with windows, not code. Maybe some point here?
                      @'Sample.exe': Loaded 'C:\Windows\SysWOW64\WSHTCPIP.DLL', Cannot find or open the PDB file
                      QVariant(Invalid)
                      'Sample.exe': Unloaded 'C:\Windows\SysWOW64\WSHTCPIP.DLL'
                      @

                      1 Reply Last reply
                      0
                      • A Almer_Cz

                        Hi guys,

                        i am quite furious about my problem and don't know how to solve it. I need to send GET request to some site and receive responce. All i get is empty string.

                        I have my MainWindow app, which has as member QNetworkAccessManager

                        Header:
                        @class MainWindow : public QMainWindow, public Ui::mainWindow
                        {
                        Q_OBJECT

                        private:
                        QNetworkAccessManager m_netAccessManager;
                        QNetworkReply * reply;

                        public:
                        MainWindow( QWidget * parent = 0 );
                        ~MainWindow();

                        private slots:
                        void httpFinished();

                        // menu slots
                        void update();
                        

                        };@

                        Source:
                        @void MainWindow::updateFonds()
                        {
                        QUrl url = QString::fromLocal8Bit("http://www.qt-project.org");
                        QNetworkRequest request(url);

                        reply = m_netAccessManager.get( request );
                        
                        QObject::connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
                        

                        };

                        void MainWindow::httpFinished()
                        {
                        // check for error
                        if ( reply->error() )
                        {
                        QString form("Download of %1 failed: %2\n");
                        this->statusbar->showMessage( form.arg(reply->url().toEncoded().constData()).arg(qPrintable(reply->errorString())) );
                        } else {
                        // get http response code
                        QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
                        QVariant contentLengthV = reply->header(QNetworkRequest::ContentLengthHeader);
                        QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);

                            if ( statusCodeV.toInt() == 302 )
                            {
                                reply->deleteLater();
                                reply = nullptr;
                        
                                QNetworkRequest request( redirectionTargetUrl.toUrl() );
                                reply = m_netAccessManager.get(request);
                                connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
                                return;
                            }
                        
                            qDebug() << statusCodeV.toString();
                            qDebug() << contentLengthV.toString();
                            qDebug() << reply->readAll();
                        }
                        
                        reply->deleteLater();
                        reply = NULL;
                        

                        };@

                        All i get is empty string. Tried wireshark to check if i am receiving response and i am. If i try to catch readyRead() signal i can read a chunk of response but i don't want to do it this way.

                        In help it says:

                        bq. if no calls to read() were made as a result of readyRead(), a call to readAll() will retrieve the full contents in a QByteArray.

                        But it doesnt apply:( Can u help me?

                        T Offline
                        T Offline
                        tongcb
                        wrote on last edited by
                        #22

                        @Almer_Cz
                        i meet the problem to with release version,
                        but with debug version every this is ok.
                        Qt 4.8.6
                        .......

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          IanMoone
                          wrote on last edited by
                          #23

                          @p3c0 said in QNetworkReply readAll() gets empty data on finished():

                          request

                          Hello,

                          I wake up this topic because I had exactly the same issue, because I may found the origin of the problem.

                          I have those error in the application output (still have them, and I use only http request) :

                          qt.network.ssl: QSslSocket: cannot resolve SSL_set_alpn_protos
                          qt.network.ssl: QSslSocket: cannot resolve SSL_CTX_set_alpn_select_cb
                          qt.network.ssl: QSslSocket: cannot resolve SSL_get0_alpn_selected
                          

                          This bug report says that those errors are relevant, but I have remove OpenVPN that use OpenSSL and OpenMG but it doesn't seems related.

                          I am using Qt 5.9.2 on Windows 10 64bits.

                          I still have few troubles to print the content, some times the print disappear or is cut, but I know that it is good with the debugger or simply because the parsing after is working fine.

                          QByteArray  content = reply->readAll();
                          
                          qDebug() << "Status Code: " << statusCode;
                          qDebug() << "Content length: " << contentLength;
                          qDebug() << "Content type: " << contentType;
                          qDebug() << "Redirect target url: " << redirectionTargetUrl;
                          
                          qDebug() << "Response:\n" << content;                   // No always full ("Response:" text can be cut too???)
                          

                          PS: Too me it seems to be black magic.

                          Serie::Serie()
                              : mNetworkAccessManager(new QNetworkAccessManager)
                          {
                              QObject::connect(mNetworkAccessManager, &QNetworkAccessManager::finished,
                                               this, &Serie::onLoadFinished);
                          }
                          
                          Serie::~Serie()
                          {
                              QObject::disconnect(mNetworkAccessManager, &QNetworkAccessManager::finished,
                                                  this, &Serie::onLoadFinished);
                              delete mNetworkAccessManager;
                          }
                          
                          void Serie::searchAddictedId()
                          {
                              mNetworkAccessManager->get(QNetworkRequest(QUrl("http://google.fr")));
                          }
                          
                          void Serie::onLoadFinished(QNetworkReply* reply)
                          {
                              if (reply->error() != QNetworkReply::NoError)
                              {
                                  qDebug() << reply->errorString();
                                  reply->deleteLater();
                                  return;
                              }
                          
                              int         statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
                              int         contentLength = reply->header(QNetworkRequest::ContentLengthHeader).toInt();
                              QString     contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
                              QUrl        redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
                              QByteArray  content = reply->readAll();
                          
                              qDebug() << "Status Code: " << statusCode;
                              qDebug() << "Content length: " << contentLength;
                              qDebug() << "Content type: " << contentType;
                              qDebug() << "Redirect target url: " << redirectionTargetUrl;
                          
                              qDebug() << "Response:\n" << content.toStdString().c_str();
                          
                              if (redirectionTargetUrl.isEmpty() == false)
                              {
                                  qDebug() << "Redirect to: " << redirectionTargetUrl;
                                  mNetworkAccessManager->get(QNetworkRequest(redirectionTargetUrl));
                                  reply->deleteLater();
                                  return;
                              }
                          
                              QXmlStreamReader    xml;
                          
                              xml.addData(content);
                          
                              ....
                          
                              reply->deleteLater();
                          }
                          
                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            Taras Ertl
                            wrote on last edited by Taras Ertl
                            #24

                            Hello,
                            I´ve had the same problem, made a request,got a reply with a 200 status code. But if i tried to qDebug() nothing would happen. However I wrote the data into a .txt file an then opened it, and the html code was there. So it seems like it is not possible for qDebug() to print the data.

                            I hope this will save some time for you guys having the same problem.

                            JKSHJ 1 Reply Last reply
                            1
                            • T Taras Ertl

                              Hello,
                              I´ve had the same problem, made a request,got a reply with a 200 status code. But if i tried to qDebug() nothing would happen. However I wrote the data into a .txt file an then opened it, and the html code was there. So it seems like it is not possible for qDebug() to print the data.

                              I hope this will save some time for you guys having the same problem.

                              JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #25

                              Welcome, and thanks for sharing your solution!

                              @Taras-Ertl said in QNetworkReply readAll() gets empty data on finished():

                              So it seems like it is not possible for qDebug() to print the data.

                              There seems to be a limit to the amount of text that can be printed to Qt Creator's "Application Output" panel. If you try to print too much text to qDebug at once, nothing will appear (I'm not sure if this is a bug or not).

                              Your workaround of printing to a text file instead is good.

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved