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. Multiple (simultaneous) Requests with QNetworkAccessManager
Qt 6.11 is out! See what's new in the release blog

Multiple (simultaneous) Requests with QNetworkAccessManager

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 4.3k Views 1 Watching
  • 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.
  • Z Offline
    Z Offline
    Zordon
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zordon
      wrote on last edited by
      #2

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

      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