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. Why i can't send more then 1 http request in parallel in the same main thread
QtWS25 Last Chance

Why i can't send more then 1 http request in parallel in the same main thread

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.7k 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.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

    Hi ,
    i just noticed that when i try to send http request while there are already http request
    going on in the background . one of them stooping and whiting until the previous will end.
    they both use there own QNetworkAccessManager.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kkrzewniak
      wrote on last edited by
      #2

      From the docs:
      Note: QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination.

      And I think it would be better to use a single QNetworkAccessManager.

      Me, Grimlock, not "nice dino". ME BASH BRAINS!

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on last edited by
        #3

        i must doing here something wrong and i don't know , here some code .
        there are 2 http post functions the first is simple post that invoked every 5 sec
        and the second one is file upload post function that call in parallel.
        when i call the file upload the first stops , whits until the file upload finished and continue this is come's from Qt with out my intervention .
        this is with single QNetworkAccessManager

        @ //Init in the class contractor

        networkManager = new QNetworkAccessManager(this);
        connect(networkManager,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(on_sslErr(QNetworkReply*,QList<QSslError>)));

        //-----------------------------------------------------\

        //this is upload file code , its taking time untill it finish working great

        PostImageRequest( QString& Response,
        QMap<QString,QString> paramsToPostMap,
        QString& BaseUrl,
        QString imageFullPath,
        int iTimeOutInterval)

        QByteArray imageFormat = QImageReader::imageFormat(imageFullPath);
        QString imageMimeType(imageFormat);
        QNetworkRequest request;
        QUrl params;
        QMapIterator<QString,QString> i(paramsToPostMap);
        while (i.hasNext()) {
        i.next();
        addField(i.key(),i.value());
        }

        addFile("file",imageFullPath,imageMimeType);
        QString crlf="\r\n";
        qsrand(QDateTime::currentDateTime().toTime_t());
        QString b=QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
        QString boundary="---------------------------"+b;
        QString endBoundary=crlf+"--"+boundary+"--"+crlf;
        QString contentType="multipart/form-data; boundary="+boundary;
        boundary="--"+boundary+crlf;
        QByteArray bond=boundary.toAscii();
        QByteArray send;
        bool first=true;

        for (int i=0; i<fieldNames.size(); i++) {
        send.append(bond);
        if (first) {
        boundary=crlf+boundary;
        bond=boundary.toAscii();
        first=false;
        }
        send.append(QString("Content-Disposition: form-data; name=""+fieldNames.at(i)+"""+crlf).toAscii());
        if (encodingS=="utf-8") send.append(QString("Content-Transfer-Encoding: 8bit"+crlf).toAscii());
        send.append(crlf.toAscii());
        send.append(strToEnc(fieldValues.at(i)));
        }
        for (int i=0; i<files.size(); i++) {
        send.append(bond);
        send.append(QString("Content-Disposition: form-data; name=""+fileFieldNames.at(i)+""; filename=""+fileNames.at(i)+"""+crlf).toAscii());
        send.append(QString("Content-Type: "+fileMimes.at(i)+crlf+crlf).toAscii());
        send.append(files.at(i));
        }

        send.append(endBoundary.toAscii());

        fieldNames.clear();
        fieldValues.clear();
        fileFieldNames.clear();
        fileNames.clear();
        fileMimes.clear();
        files.clear();
        request.setHeader(QNetworkRequest::ContentTypeHeader, contentType.toAscii());
        request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(send.size()).toString());
        request.setUrl(BaseUrl);

        if(iTimeOutInterval != -1)
        {
        QEventLoop loop2;
        QTimer::singleShot(iTimeOutInterval, &loop2, SLOT(quit()) );
        loop2.exec();
        }
        QEventLoop loop;
        QNetworkReply *reply = networkManager->post(request,send);
        connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this,SLOT(SetProgress(qint64,qint64)));
        connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec();
        //return response
        QNetworkReply::NetworkError networkError = reply->error();
        HandleNetworkError(networkError);
        Response.clear();
        QByteArray data=reply->readAll();
        Response.append(data);
        //delete reply;
        reply->deleteLater();

        //--------------------------------------------------------------------------------\

        //this is the post function that invoket every 5 secound
        

        PostRequest(QString& Response,
        QMap<QString,QString> paramsToPostMap,
        QString& BaseUrl,
        int iTimeOutInterval)

        QNetworkRequest request;
        QUrl params;
        QMapIterator<QString,QString> i(paramsToPostMap);
        while (i.hasNext()) {
        i.next();
        params.addQueryItem(i.key(),i.value());
        }
        request.setUrl(BaseUrl);

        QByteArray postArgs;
        postArgs = params.encodedQuery();

        if(iTimeOutInterval != -1)
        {
        QEventLoop loop2;
        QTimer::singleShot(iTimeOutInterval, &loop2, SLOT(quit()) );
        loop2.exec();
        }
        QEventLoop loop;
        QNetworkReply *reply = networkManager->post(request,postArgs);
        connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec();
        //return response
        QNetworkReply::NetworkError networkError = reply->error();
        HandleNetworkError(networkError);
        Response.clear();
        QByteArray data=reply->readAll();
        Response.append(data);

        //delete reply;
        reply->deleteLater();@

        1 Reply Last reply
        0
        • L Offline
          L Offline
          loladiro
          wrote on last edited by
          #4

          I'm 99% sure that it's because you're using two QEventLoops (I don't know exactly why it's hapenning and it might have something to do with the code you use to call the second function ever 5 seconds). Therefore, yous should probably use two separate slots for handling the replies instead of the event loops.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on last edited by
            #5

            but its is 2 functions , the scope of the QEventloop is local to its function

            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