K
K
klajowski2021-05-12 22:54:43
Qt
klajowski, 2021-05-12 22:54:43

How to handle multiple get requests in QNetworkAccessManager?

I have a certain number of links (more than 6) for which I want to get the html code of these pages.
And I have to glue them in the process of obtaining. The only question is, how do I send these requests and how do I process them in the slot so that I can end up with a page that is in the same order as the links?

I am trying to do the following:

void MyLoader::loadPageByLinks()
{
    // links  - это QStringList
    for(const QString& link : links)
    {
        manager->get(QNetworkRequest(QUrl(link)));
    }
}


In the MyLoader constructor:
manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyReady(QNetworkReply*)));

In replyReady():
void MyLoader::replyReady(QNetworkReply *reply)
{
     QString currentPage(reply->readAll());

     ... // делаю преобразования currentPage с помощью регулярных выражений

     finalPage.append(currentPage); // добавляю к финальной странице текущую (псевдокод)
     
}


One of the fields of the MyLoader class is the completed finalPage. Also, there is a public method that returns this page (const QString&), which will need to be used in another class for display.

I suspect I'm doing it all wrong. Nothing comes to mind.

Please tell me how to solve this problem. Thank you.

ps I'm still quite green in programming and don't know much, but the deadlines are running out and I need to solve the problem as quickly as possible.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2021-05-12
@klajowski

get( and post( return a reference to the QNetworkReply that comes in the response.
You can use setProperty to set your own values.
This way you can identify which request the response came from.
The order of responses for multiple requests may be different, since QNetworkAccessManager uses 4 connections at the same time .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question