0

I've got a problem trying to send a request using QNetworkAccessManager from a QObject derived class.

Firstly in my constructor I do the following:

QObject::connect( &mAccessManager, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( requestFinished( QNetworkReply* ) ) );

Then when I wish to send the request I do the following:

QNetworkRequest checkLogin( QUrl( address ) );
checkLogin.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" );

mByteArray  = QByteArray( "POST \"client\" : oxres" );
QNetworkReply* pReply   = mAccessManager.post( checkLogin, mByteArray );

Now at this point I expect to wait around for a few seconds and then receive a response to my class's slot requestFinished. It doesn't however get called.

I'm at a loss as to what I'm doing wrong. Anyone got any ideas? (I'm sure its something stupidly simple).

Edit: Ok this is very weird. I have wireshark collecting packets in the background and when i generate that post request I see nothing happening in wireshark. So why isn't it sending??

Edit 2: Hmm it seems that the problem lies with me sending a request to an https:// address ... why would that be??

Goz
  • 61,365
  • 24
  • 124
  • 204

3 Answers3

3

A few points:

1) The thread your QNetworkRequest belongs to must be running event loop (only this way you can receive signals).

2) If you "wait around" with something like sleep after mAccessManager.post it is not going to work. You have to let event loop iterate to process signals, etc. You can run event loop yourself after post.

ADD:

I did it synchronously like this

QNetworkRequest request;

request.setHeader(
            QNetworkRequest::ContentTypeHeader,
            QVariant( QString("text/xml") )
            );
request.setHeader(
            QNetworkRequest::ContentLengthHeader,
            QVariant( qulonglong(mesPOST.size()) )
            );
request.setHeader(...);
//etc....

request.setAttribute(
            QNetworkRequest::CacheLoadControlAttribute,
            QVariant( int(QNetworkRequest::AlwaysNetwork) )
            );
request.setUrl( QUrl( "http://bla.bla", QUrl::StrictMode ) );

QNetworkReply* pReply = m_NetMgr->post( request, mesPOST );
QEventLoop eLoop;

QObject::connect( pReply, SIGNAL(finished()), &eLoop, SLOT(quit()) );
eLoop.exec( QEventLoop::ExcludeUserInputEvents );
Serge Dundich
  • 4,221
  • 2
  • 21
  • 16
  • Its running on my main UI thread. After the post call the function returns and control returns to the main UI thread. Still don't get the signal though :( – Goz Jun 02 '11 at 13:13
0

I think this is because QNetworkAccessManager is asynchronous and need the time to send the request. But require, url, query, ... is destroyed when out of scope.
-> we need in scope when QNetworkAccessManager do send request (use QEventLoop().exec(); ) or let require, url, query, ... is persistent (declare it with static). See: Qt: QNetworkAccessManager dont send request

Community
  • 1
  • 1
aviit
  • 1,957
  • 1
  • 27
  • 50
0

Ok so it turns out the problem is caused byt the fact that Qt does not include SSL support by default.

http://doc.qt.nokia.com/4.7/ssl.html

user240515
  • 3,056
  • 1
  • 27
  • 34
Goz
  • 61,365
  • 24
  • 124
  • 204
  • 2
    The page you are referencing states that QT DOES support SSL by default. But it loads OpenSSL DLL dynamically by default which means that OpenSSL DLL have to be available in you system when you run your executable. Alternatively you may compile QT to import OpenSSL symbols (instead of dynamically loading them at run-time) which means that if you link against DLL version of OpenSSL (when you build QT DLLs) your executable won't even run without OpenSSL DLL available. Or if you link QT DLLs with static OpenSSL then you won't need OpenSSL DLL at all. – Serge Dundich Jun 02 '11 at 20:12