QNetworkReply finished signal always gots empty data
-
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
-
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 ?
-
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. -
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.@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. -
Hi
So same code works with 1909 winer, but reads nothing on 2004 winver ?
(and no error reported)
Is that correctly undestood? -
Hi
So same code works with 1909 winer, but reads nothing on 2004 winver ?
(and no error reported)
Is that correctly undestood?@mrjj @Christian-Ehrlicher Sorry, this is my mistake. After readall once, reading the data again is empty. This is correct.