Solved How to get information from QNetworkReply
-
Hi,
I am using QNetworkManager, and i want to make a get request to an url and print the reply information to a file, but i seem to have a problem with my reply as the information is not available and the ReadyRead or Finished signals are not being triggered.
std::string url = ui.searchLine->text().toStdString(); QUrl myurl = QUrl(url.c_str()); QNetworkReply* reply = man->get(QNetworkRequest(myurl)); connect(reply, &QNetworkReply::readyRead, [&]() { QByteArray read = reply->readAll(); QFile out("page.html"); out.open(QIODevice::WriteOnly|QIODevice::Text); out.write(read); out.close(); reply->close(); reply->deleteLater(); });
-
Hi
Maybe try using QUrl
manager->get(QNetworkRequest(QUrl(myurl)));https://www.bogotobogo.com/Qt/Qt5_Downloading_Files_QNetworkAccessManager_QNetworkRequest.php
-
It seems to have no effect, and i can't seem to be able to see the value of reply!(https://ibb.co/BtYTBy0)
-
@undac
well now it enters the ReadReady it seems ?
So check the errors as seen in link.
maybe be some error if read variable is empty.if(reply->error()) { qDebug() << "ERROR!"; qDebug() << reply->errorString(); }
-
When running it now though, it throws me an acces violation reading location exception at the reply->readAll()
-
Hi
Then there must be something with the capture of reply variable if its
invalid inside the lambda.try
[=]
and not [&]
as maybe it captures the local reply and its out of scope when lambda runs. -
@mrjj said in How to get information from QNetworkReply:
as maybe it captures the local reply and its out of scope when lambda runs.
[&] captures by reference and the local pointer is no longer valid due to this.
[=] is also not really useful - simply copying all is like using a hammer to open an egg... better is to only copy what you really need -> [this, reply] -
It does work both with the [=] syntax and the [this, reply] one, but i opted for the latter. I noticed that it didn't download the data properly and so i changed to &QNetworkReply::finished and it works as desired.
Thank you very much!
-
@undac Then please mark this topic as solved, thx.