QNetworkReply readAll() gets empty data on finished()
-
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_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);
~Dialog();public slots:
void onFinished(QNetworkReply *reply);private:
Ui::Dialog *ui;
QNetworkAccessManager m_qnam;
};#endif // DIALOG_H
@ -
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();
}
@ -
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....
-
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. -
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..
-
That's weird. It works perfectly on my system, Ubuntu 14.04. Try removing reply->deleteLater() from onFinished(QNetworkReply* reply) slot.
-
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...
-
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. -
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().
-
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'
@ -
@Almer_Cz
i meet the problem to with release version,
but with debug version every this is ok.
Qt 4.8.6
....... -
@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(); }
-
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.
-
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.