Multiple (simultaneous) Requests with QNetworkAccessManager



  • Hi,
    I want to make multiple requests with QNetworkAccessManager in a loop: The class CScriptUpdater is derived from QNetworkAccessManager and uses it's get() function to make the requests.

    @void CScriptUpdater::OnFinishedCheckingScriptUpdates()
    {
    connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnFinishedScriptDownload(QNetworkReply*)));
    // ...
    foreach (const QString &script, scriptList)
    {
    // ...
    QString sUrl = SCRIPT_UPDATE_URL;
    sUrl = sUrl.arg(currentScript[0]);
    QNetworkRequest request = QNetworkRequest(QUrl(sUrl));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain; charset=UTF-8");
    QNetworkReply* pNetworkReply = get(request);
    CScriptUpdater* updater = new CScriptUpdater(m_pHttpManager, currentScript[0], pNetworkReply);
    }
    }@

    Here the constructor and the connected slot:
    @CScriptUpdater::CScriptUpdater(CHttpManager* pHttpManager, QString host, QNetworkReply *pReply)
    {
    m_pHttpManager = pHttpManager;
    m_sHost = host;
    m_pNetworkReply = pReply;
    }

    void CScriptUpdater::OnFinishedScriptDownload(QNetworkReply *pReply)
    {
    QString sReply = QString(pReply->readAll());
    QNetworkRequest req = pReply->request();
    QString host = req.url().toString();
    }@

    The problem is:
    QString sReply is always empty except for the last request made in the foreach loop. How do I correctly make multiple asynchronous requests? Thanks

    edit. the creation of new CScriptUpdater objects in the loop is actually not necessary. Previously I used the finished() Signal of QNetworkReply (instead of QNetworkAccessManager as posted above) and so I had to store all reply objects... But the result was the same.



  • Solved it by creating a new QNetworkAccessManager AND objects to connect to in every loop cycle.


Log in to reply
 

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