Synchronous QNetworkAccessManager get



  • 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.



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.