PDA

View Full Version : QNetworkAccessManager, not using it right?



Cruz
24th November 2016, 11:08
Hi!

I want to write a program that issues get, put, and delete HTTP requests. I tried to use the QNetworkAccessManager to accomplish this, but somehow not even a simple example works for me. Here is my code:



QNetworkAccessManager nm;
QNetworkReply* nr = nm.get(QNetworkRequest(QUrl(QString("http://www.yahoo.com"))));
while (!nr->isFinished())
{
sleep(10);
qDebug() << nr->isFinished() << nr->error() << (nr->request()).url();
}


And the output is:



qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
false 0 QUrl( "http://www.yahoo.com" )
false 0 QUrl( "http://www.yahoo.com" )
false 0 QUrl( "http://www.yahoo.com" )
false 0 QUrl( "http://www.yahoo.com" )
false 0 QUrl( "http://www.yahoo.com" )
false 0 QUrl( "http://www.yahoo.com" )
false 0 QUrl( "http://www.yahoo.com" )


First of all, what are the SSL socket warnings about? I did not want to use an SSL connection here.
I used wireshark to observe the network traffic, and can see that not a single packet leaves my interface when using this code. It is not working.

And yes, I do not want to use the QNetworkManager in an asynchronous mode, I would rather wait in a loop until it is finished.

Can anyone tell me what I'm doing wrong?

Thanks
Cruz

anda_skoa
24th November 2016, 13:17
When you "sleep()" you are blocking the thread, it can't do any of the operations you are asking it to perform.

You need to allow the Qt event loop to process events.

One option is to use a nested event loop



QEventLoop loop;
connect(nr, SIGNAL(finished()), &loop, SLOT(quit())); // or the equivalent function pointer based syntax
loop.exec();

Cruz
24th November 2016, 14:15
Okay this works. What are the SSL warnings about? How do I get rid of them?

anda_skoa
24th November 2016, 15:48
Not sure, maybe a missing OpenSSL library?

But since you don't need SSL, you can just ignore those.

Cheers,
_