Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Synchronous QNetworkAccessManager get
QtWS25 Last Chance

Synchronous QNetworkAccessManager get

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 13.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mirswith
    wrote on last edited by
    #1

    Hi. I am trying to create a synchronous call to an http get but am having trouble. It seems to immediately exit with an empty _response and no errors. Here is my code:

    @
    bool ScriptWebRequest::get(const QString & url)
    {
    _response.clear();
    _request.setUrl(url);
    _netReply = _netMan->get(_request);

    while( _netReply->waitForReadyRead(_timeout) )
    {
    _response.append( _netReply->readAll() );
    }

    _error = _netReply->error();
    _errorString = _netReply->errorString();

    return _error==QNetworkReply::NoError;
    }
    @

    Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mirswith
      wrote on last edited by
      #2

      from doing a little googling I found that replacing my while with:

      @
      QEventLoop loop;
      connect(_netReply, SIGNAL(finished()), &loop, SLOT(quit()));
      loop.exec();
      @

      works and while I understand why this works I do not understand why my original code does not. It seems waitForReadyRead is broken, which according to the docs says it will block until it either times out or an error has occurred.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        QNetworkReply does not implement the waitFor* methods (and: they do nothing in the base class implementation). You must use the async interface. The QEventLoop is a nice trick but may pose other problems.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved