Results 1 to 3 of 3

Thread: posting to an invalid URL using QNetworkAccessManager

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default posting to an invalid URL using QNetworkAccessManager

    Hi Everyone, I have a simple requirement on my Arm-Linux embedded platform wherein I need to post to a URL and wait for the reply. I achieve that using the following code-
    Qt Code:
    1. void MyApp::MyApp()
    2. {
    3. m_networkManager = new QNetworkAccessManager(this); // Instance variable
    4. connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
    5. this, SLOT(onRequestCompleted(QNetworkReply *)));
    6. }
    7.  
    8. void MyApp::getData()
    9. {
    10. QByteArray dataToBePosted;
    11. QNetworkRequest request;
    12. request.setUrl(QUrl("http://www.domain.foo"));
    13. m_networkManager->post(request,dataToBePosted);
    14. }
    15.  
    16. void MyApp::onRequestCompleted(QNetworkReply *reply)
    17. {
    18. QByteArray data = reply->readAll();
    19. reply->deleteLater();
    20. }
    To copy to clipboard, switch view to plain text mode 

    I have 2 doubts regarding usage of QNetworkAccessManager class-
    1) What I observed is that if I post to a valid URL I immediately get a response, but if the URL is invalid then it takes some time for the reply to come (sometimes like 40 secs).
    Moreover, if the URL is invalid then I observe an increase in memory usage in my application after the following line-
    Qt Code:
    1. m_networkManager->post(request,dataToBePosted);
    To copy to clipboard, switch view to plain text mode 
    I know this sounds very strange but this is what is happening. If the URL exists then everything is fine, but by mistake if I post to an invalid URL then my memory usage immediately shoots up

    2) In Qt Documentation it is mentioned that
    One QNetworkAccessManager should be enough for the whole Qt application.
    Suppose I use 2 QNetworkAccessManager objects in my application, will that create any issue?

    I would be really glad if someone would clarify my doubts.
    Last edited by sattu; 8th January 2014 at 15:39. Reason: Mistake in code

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: posting to an invalid URL using QNetworkAccessManager

    You needlessly create a new QNetworkAccessManager on each call to getData()
    Moreover, you never delete it. So each call to getData() leaks a QNetworkAccessManager instance.

    Since you already have m_networkManager as an instance member, only create one QNAM instance and re-use it.

    Regarding 2 QNAM: that is possible and does not create issues. Using one QNAM is usually more efficient since the instance can then do all kinds of tricks to optimize e.g. connects, like re-using sockets, re-using SSL sessions, etc.

    See Peter Hartmann's presentation at this year's Qt Developer Days: http://devdays.kdab.com/wp-content/u...k-features.pdf

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sattu (8th January 2014)

  4. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: posting to an invalid URL using QNetworkAccessManager

    Quote Originally Posted by anda_skoa View Post
    You needlessly create a new QNetworkAccessManager on each call to getData()
    Moreover, you never delete it. So each call to getData() leaks a QNetworkAccessManager instance.
    Since you already have m_networkManager as an instance member, only create one QNAM instance and re-use it.
    _
    Am extremely sorry for the confusion, actually I have only one instance of QNAM running and I keep re-using it. I modified the code to put it here and so did that mistake. Have rectified it, thanks for pointing it out.

Similar Threads

  1. Job Posting - Software Developer (C++, Qt)
    By Cedric Matteo in forum Jobs
    Replies: 1
    Last Post: 26th October 2010, 13:25
  2. Posting data to QWebView
    By chrisb123 in forum Newbie
    Replies: 5
    Last Post: 29th October 2009, 06:58
  3. Posting a QKeyEvent to a QLineEdit
    By cocheci in forum Qt Programming
    Replies: 14
    Last Post: 5th June 2006, 14:54

Tags for this Thread

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.