Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkReply finished signal always gots empty data
QtWS25 Last Chance

QNetworkReply finished signal always gots empty data

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 712 Views
  • 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.
  • D Offline
    D Offline
    Dylan Deng
    wrote on last edited by
    #1

    This is my code:

    class HttpManager : public QObject
    {
        Q_OBJECT
    public:
        HttpManager(QObject* parent = nullptr) : m_httpManager(new QNetworkAccessManager(parent))
        {
            connect(m_httpManager, &QNetworkAccessManager::finished, this, &HttpManager::handleFinished);
        }
        void postRequest()
        {
            QNetworkRequest request;
            request.setUrl(QUrl("https://api.ipify.org?format=json"));
            QNetworkReply* reply = m_httpManager->get(request);
    
            connect(reply, &QNetworkReply::finished, [=]() {
                qInfo() << "------------------- reply:" << reply->readAll();
            });
            connect(reply, &QIODevice::readyRead, [=]() {
                qInfo() << "------------------- ready read:" << reply->readAll();
            });
        }
    public slots:
        void handleFinished(QNetworkReply* reply)
        {
            qInfo() << "------------------- access manager finished slot:" << reply->readAll();
        }
    
    private:
        QNetworkAccessManager* m_httpManager;
    };
    

    My test window constructor:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        HttpManager* httpManager = new HttpManager(this);
        httpManager->postRequest();
    }
    

    In this case, the QNetworkReply always gots empty data in QNetworkReply::finished signal and QNetworkAccessManager::finished signal. Result like this:

    ------------------- ready read: "{\"ip\":\"103.126.92.85\"}"
    ------------------- access manager finished slot: ""
    ------------------- reply: ""
    

    This happens only on individual computers, and I confirm that there is no problem with my computer's network. How should I use it?

    Windows 10 1909 Home, Windows 10 2004 Home

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Did you inspect it for erros using the signal ?
      connect(reply, &QNetworkReply::errorOccurred,
      this, &MyClass::slotError);

      • This happens only on individual computers

      Im not sure what that means. All computers are individual :)

      Can you give better details on where/when this does happen and what it does not ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dylan Deng
        wrote on last edited by Dylan Deng
        #3

        No errors are output in errorOccurred, This problem occurs on my desktop computer, but there is no problem with my laptop.
        After my laptop was replaced with Windows 10 2004 Home version system, this problem also appeared in my laptop.

        Christian EhrlicherC 1 Reply Last reply
        0
        • D Dylan Deng

          No errors are output in errorOccurred, This problem occurs on my desktop computer, but there is no problem with my laptop.
          After my laptop was replaced with Windows 10 2004 Home version system, this problem also appeared in my laptop.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Dylan-Deng I don't see why you think something is wrong here - readAll() returns the data you want, or?

          QByteArray QIODevice::readAll():
          Reads all remaining data from the device, and returns it as a byte array.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            So same code works with 1909 winer, but reads nothing on 2004 winver ?
            (and no error reported)
            Is that correctly undestood?

            D 1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              So same code works with 1909 winer, but reads nothing on 2004 winver ?
              (and no error reported)
              Is that correctly undestood?

              D Offline
              D Offline
              Dylan Deng
              wrote on last edited by
              #6

              @mrjj @Christian-Ehrlicher Sorry, this is my mistake. After readall once, reading the data again is empty. This is correct.

              1 Reply Last reply
              1

              • Login

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