QNetworkReply readall
-
Hi, i see QNetworkReply can read content of website.
Because my index.php contain about 5 numbers i try to use it.
Code:
@void download()
{
QUrl url("http://website.com/index.php");
url.adQueryItem("version", 2); // = index.php?version=2QNetworkRequest netRequest(url);
netReply = netManager.get(netRequest); // netReply = QNetworkReply, netManager = QNetworkAccessManager
connect(netReply, SIGNAL(readyRead()), SLOT(read());
}void read()
{
QString content = netReply->readAll();
QMessageBox::information(0, "Info", content); // Web url content "987 654 321" But it show just "1"
}@Any idea how to solve?
-
QIODevice::readyRead() gets emitted whenever new/more data is available on the QIODevice, that is not necessarily all data. If you want to read all data in one go you rather want to connect to QNetworkReply::finished() but please bare in mind that all your data has to fit into memory for this to work.
-
Use "finished()":http://doc-snapshot.qt-project.org/4.8/qnetworkreply.html#finished SLOT instead of readyResd()
-
Try this, and start you App from console or run directly from IDE with opened output window.
If it doesn't help, and the SIZE != BODY length, check encoding, take wireschark and track back communication etc...@
#include <QDebug>void read()
{
QString content = netReply->readAll();qDebug() << "SIZE: " << netReply->bytesAvailable();
qDebug() << "BODY: " << QString(netReply->readAll());QMessageBox::information(0, "Info", content);
}
@ -
I had the same issue. The QIODevice::readAll() (which is called from QNetworkReply) function states this issue:
This function has no way of reporting errors; returning an empty QByteArray can mean either that no data was currently available for reading, or that an error occurred. This function also has no way of indicating that more data may have been available and couldn't be read.
The resolution is to connect QIODevice::readyRead to as per the documentation:
connect(reply, &QIODevice::readyRead, this, [=]() { QByteArray response = reply->readAll(); qDebug() << response; });