Results 1 to 2 of 2

Thread: Problems with QNetworkAccessManager

  1. #1
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Problems with QNetworkAccessManager

    Hi all,

    I'm having some problems with QNetworkAccessManager (qt 4.5.2, windows 7). i try to develop a simple widget that is supposed to connect to google calendar. Basically all functionality is a set of post() and request() function calls to the appropriate google calendar urls. While this basically works, i get random http errors at random points of execution, and i cant seem to find out whats wrong.

    my current code is supposed to do a 'login', consisting of 3 requests:
    1) post() username & password to login url
    * receive authorization token, use this for all following requests
    2) get() to calendar list url, to receive a list of calendars
    * receive calender list
    3) get() to event list url
    * receive event list

    sometimes, all steps are executed, and sometimes i get http400/Bad Request, http 399/Data corrupted or even http status code 0 along with an empty reply at random points. now, i think it might have something to do with the fact that i fire requests in QNetworkAccessManager's finished() slot, but i dont know how to do it right.

    now for some code:

    Qt Code:
    1. MainClass::MainClass()
    2. {
    3. lastRequest = 0;
    4. manager = new QNetworkAccessManager(this);
    5. connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));
    6. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
    7. connect(manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), this, SLOT(onProxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
    8. connect(manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(onSslErrors(QNetworkReply*,QList<QSslError>)));
    9.  
    10. ...
    To copy to clipboard, switch view to plain text mode 

    prepare Requests:
    Qt Code:
    1. int MainClass::prepare(const QString& url)
    2. {
    3. delete lastRequest;
    4. lastRequest = new QNetworkRequest(QUrl(url));
    5.  
    6. if (!authToken.isEmpty())
    7. {
    8. lastRequest->setRawHeader("Authorization", authToken);
    9. lastRequest->setRawHeader("GData-Version", "2");
    10. }
    11.  
    12. return success;
    13. }
    To copy to clipboard, switch view to plain text mode 

    do login:

    Qt Code:
    1. int MainClass::login(const QString& aEmail, const QString& aPassword)
    2. {
    3. DataPacket data; // request body
    4.  
    5. prepare(urls[urlLogin]); // s.o.
    6.  
    7. /* prepare body */
    8.  
    9. manager->post(*lastRequest, data.getData() /* == QByteArray */);
    10.  
    11. return success;
    12. }
    To copy to clipboard, switch view to plain text mode 

    the other requests are simple GETs on the apropriate feed-urls:

    Qt Code:
    1. int MainClass::getCalendarList()
    2. {
    3. ...
    4. /* some error checks */
    5.  
    6. prepare(urls[urlCalendarListFull]);
    7.  
    8. manager->get(*lastRequest);
    9.  
    10. return success;
    11. }
    To copy to clipboard, switch view to plain text mode 

    finished-slot:

    Qt Code:
    1. void MainClass::onFinished(QNetworkReply* reply)
    2. {
    3. int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    4.  
    5. qDebug() << "FINISHED" << statusCode;
    6.  
    7. switch (statusCode)
    8. {
    9. case 200: // OK
    10. {
    11. // process reply
    12.  
    13. if (!isLoggedIn())
    14. storeAuthorization(reply);
    15.  
    16. switch (state)
    17. {
    18. case stGetCalendars:
    19. {
    20. parseCalendarList(reply);
    21. break;
    22. }
    23. case stGetEvents:
    24. {
    25. parseEventList(reply);
    26. break;
    27. }
    28. }
    29.  
    30. break;
    31. }
    32.  
    33. case 302: // redirect (Location: [URL])
    34. {
    35. qDebug() << "redirected to" << locationOf(reply);
    36. prepare(locationOf(reply));
    37. manager->get(*lastRequest);
    38.  
    39. break;
    40. }
    41.  
    42. case 400: // bad request
    43. {
    44. QMessageBox::warning(this, "Error", "Network error (HTTP400/Bad Request)");
    45. break;
    46. }
    47.  
    48. case 403: // permission denied
    49. {
    50. QMessageBox::warning(this, "Permission denied", "Username or password invalid");
    51. break;
    52. }
    53.  
    54. default:
    55. {
    56. QByteArray replyData = reply->readAll();
    57. qDebug() << "Unknown response" << replyData;
    58. break;
    59. }
    60. }
    61.  
    62. reply->deleteLater();
    63.  
    64. // do anything afterwards?
    65.  
    66. after(); // <-- next request via manager->get()
    67.  
    68. qDebug() << "request done";
    69. }
    To copy to clipboard, switch view to plain text mode 

    does someone know what might be wrong?

  2. #2
    Join Date
    Jul 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QNetworkAccessManager

    anyone please?

Similar Threads

  1. Mac OSX OpenGL problems
    By tksharpless in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 18:27
  2. problems creating toolbar...(do see the attachment)!
    By sumit in forum Qt Programming
    Replies: 15
    Last Post: 10th September 2008, 12:23
  3. Problems with scope and C header functions
    By waldowoc in forum Newbie
    Replies: 5
    Last Post: 5th August 2008, 12:29
  4. flicker and wierd resize problems ...
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2008, 19:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.