No reply or error from QNetworkAccessManager GET request
-
Hello :)
I'm writing an app to get a temperature logging file from and arduino(esp8266) board which is on the same network as my notebook. I wrote the code on the board for sending the file when a GET request is coming, by using client.write(filename) command. In this way, the content of the file is sent as response.When the file has around 500 lines, I can successfully display the content of the file in qt. However, if the file has more than 500 lines, I don't get any reply in qt anymore. Chrome and curl can still get the response, even if the file has 10000 lines. It takes them around a second to receive the response.
The QNetworkAccessManager::finished signal is never fired anymore. The same goes for QNetworkReply::NetworkError signal. Any ideas where to problem might be ? -
Here is the code. The download starts when I click on a button in the gui.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), nam(new QNetworkAccessManager) { ui->setupUi(this); connect(nam,&QNetworkAccessManager::finished,[](QNetworkReply* r){ qDebug()<<QString(r->readAll()); r->deleteLater(); }); } void MainWindow::on_pushButton_clicked() { connect(nam->get(QNetworkRequest(QUrl("http://192.168.1.103"))),static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),[](QNetworkReply::NetworkError err){ qDebug()<<err; }); }
-
@cpper said in No reply or error from QNetworkAccessManager GET request:
Ok, so it works if the response has 32763 characters or less. That's quite close to 2^15(32768) so maybe it makes sense to anyone.
interesting. if nobody here knows an answer, I'd file this at bugreports.qt.io
It may be related to your environment, but maybe the network developers have some hint for you.
-
So the report is: https://bugreports.qt.io/browse/QTBUG-65755
-
Indeed :)
It's my first time I submit to bugreports.qt.io so I hope I did it right. -
Check your reply buffer size. There are two options if you're running out of buffer space:
Set a bigger buffer with setReadBufferSize(). The documentation is kinda vague but it mentions that the available bytes can be larger than the buffer size (which I'm guessing makes the request sorta hang there until you make some more room in the buffer or it timeouts).
Instead of waiting for the whole thing to arrive connect to the
readyRead()
signal and read the data (i.e. empty the buffer) as it comes in chunks. -
Thanks Chris. It seems that setReadBufferSize() dosen't change anything. I tried to set it to the exact size of the incoming reply, or to a bigger value, but I get the same behavior.
It seems I can get the entire reply, in chunks, using readyRead signal. Printing readBufferSize() in the readyRead slot always prints 0, if I don't set in manually via setReadBufferSize().
I would still be happy to be able to read the entire reply via finished signal so I'll try some more things. I got a reply in my bugreport(link above), I tried setting up the headers, but still, the same result. -
I made a mistake and wasted your and my time. When I saw that the
qDebug()<<QString(r->readAll());
line in the finished slot doesn't print anything, I tried doing something like
qDebug()<<"test"<<QString(r->readAll());
trying to see if the slot actually gets called at all. Since I got the same (lack of) any output(also no "test"), I assumed that the slot is never called. But I was wrong.
The problem has nothing to do with networking, it seems it's all about qDebug. I was trying to get rid of the \r\n characters(display them as newlines) and used QTextStream(stdout) instead of qDebug(). Not only I was able to display the mentioned characters as a newline in the output pane, but was also surprised to see the content from bigger files too. So it seems it's actually qDebug's << overload who couldn't handle a longer input.
Doing
qDebug()<<"test"; qDebug()<<QString(r->readAll());
in the finished slot, or doing some proper debugging with a breakpoint would have saved me(and you) some time and effort.
So finally, the "issue":
QTextStream out(stdout); QString str1(32763,'X'); QString str2(32764,'Y'); qDebug()<<str1; //works qDebug()<<str2; //no output out<<str2; //works
I posted the same message in the bugreport.
Thanks aha_1980 and Chris for your support :) -
@cpper said in No reply or error from QNetworkAccessManager GET request:
qDebug()<<QString(r->readAll());
Please be aware that if you do this readAll() for qDebug the next call to readAll() will NOT return anything as the first one already returned the content!