hello,

I have a class that communicates with a REST interface using the QNetwork stack.
Here is some example code to make a HTTP request to the server that calls 'serverRequest()' when finished and 'requestError()' on error:

Qt Code:
  1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  2. connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(serverRequest(QNetworkReply *)));
  3. QNetworkReply *reply = manager->get(QNetworkRequest(url));
  4. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
  5.  
  6.  
  7. void serverRequest(QNetworkReply *r) {
  8. if(r->error() == QNetworkReply::NoError) {
  9. ...
  10. }
  11. delete r->manager();
  12. }
  13.  
  14. void requestError(QNetworkReply::NetworkError e) {
  15. switch(e) {
  16. case QNetworkReply::ConnectionRefusedError:
  17. ...
  18. }
  19. }
To copy to clipboard, switch view to plain text mode 

My problem is that for certain errors (such as timeout but not 404) the error() signal is emitted but not finished().
The error signal only passes an integer of the type of error so there is no direct way to tell which request failed. Also I can't clean up the memory used by the request.

Currently I get around this with a QMap that stores when a request is made with a pointer to the QNetworkRequest instance. Then I use a QTimer to periodically retry unfinished requests.
But this feels like a big hack. Is there a more elegant and flexible way to make network requests reliably?

Richard