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. network request will always timeout with a long time server return?

network request will always timeout with a long time server return?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 318 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.
  • N Offline
    N Offline
    nelson.W
    wrote on 25 Jun 2024, 01:45 last edited by
    #1

    first of all ,sorry for my bad english..
    if server takes a long time to return,more than 300 seconds,QT request will timeout,reply will abort.
    for eg.
    php server code.server will return after 6*60 seconds .

    <?php  
    sleep(60*6); 
    echo "test"; 
     die();
    ?>
    

    qt codes

    #include <QCoreApplication>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkReply>
    #include <QtNetwork/QNetworkRequest>
    #include <QDebug>
    #include <QObject>
    #include <QEventLoop>
    #include <QDateTime>
    
    void replyFinished(){
        QDateTime dateTime;
        qDebug() << "replyFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    }
    void managerFinished(){
        QDateTime dateTime;
        qDebug() << "managerFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    }
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QNetworkAccessManager manager;
        manager.setTransferTimeout(60*1000 * 7);// 7 minutes timeout
        QNetworkReply *reply;
        QNetworkRequest request;
    
        request.setTransferTimeout(60*1000 * 7);// 7 minutes timeout
        request.setUrl(QUrl(u8"https://www.dajuan.com/home/test"));// 6 minutes return "test"
        request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::NoLessSafeRedirectPolicy);
    
    
        reply = manager.get(request);
    
        QObject::connect(reply,&QNetworkReply::finished,&a,replyFinished);
    
        QDateTime dateTime;
        qDebug() << "reuqest begin " << dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        QEventLoop eventLoop;
        //更新进度条
        QObject::connect(&manager,&QNetworkAccessManager::finished,&eventLoop,&QEventLoop::quit);
        QObject::connect(&manager,&QNetworkAccessManager::finished,&a,managerFinished);
    
        //block until finish
        eventLoop.exec();
    
        qDebug() << "reuqest done "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    
        if(reply->error() == QNetworkReply::NoError) {
            QByteArray data = reply->readAll();
            QString s = QString::fromStdString(data.toStdString());
            qDebug() << "reply string " << s;
        }
        else{
            QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
            QString msg = QString("code:%1 \ninfo:%2").arg(statusCode.toInt()).arg(reply->errorString());
            qDebug() << "reply error string " << msg;
        }
    
        return a.exec();
    }
    
    

    debug output is like this,we can see that the request is [timeout,after 7 miniutes]

    reuqest begin  "2024-06-25 09:32:15"
    managerFinished  "2024-06-25 09:39:15"
    replyFinished  "2024-06-25 09:39:15"
    reuqest done  "2024-06-25 09:39:15"
    reply error string  "code:0 \ninfo:Operation canceled"
    

    if server return time is shorter than 300s ,debug output is down here ..I performs well.

    reuqest begin  "2024-06-25 09:42:48"
    managerFinished  "2024-06-25 09:43:49"
    replyFinished  "2024-06-25 09:43:49"
    reuqest done  "2024-06-25 09:43:49"
    reply string  "test"
    

    I know that ,QNetworkAccessManager and QNetworkRequest default timeout is 300s.(Also,I don't konw the difference of these two timeout...)
    my qt version is 5.15.2 ,and 5.15.14(i thought it's a bug,so I upgrades to 5.15.14..but, these 2 version is the same result)

    So,this is a bug,or I used this in a wrong way?

    J 1 Reply Last reply 25 Jun 2024, 11:49
    0
    • N Offline
      N Offline
      nelson.W
      wrote on 25 Jun 2024, 02:03 last edited by
      #2

      I mean ,if server return time is longer than 300 seconds,QT will always Timeout,Operation canceled!
      I set 7 miniutes timeout .If server returns at 5 minutes,Qt will go to 7 minutes...

      1 Reply Last reply
      0
      • N nelson.W
        25 Jun 2024, 01:45

        first of all ,sorry for my bad english..
        if server takes a long time to return,more than 300 seconds,QT request will timeout,reply will abort.
        for eg.
        php server code.server will return after 6*60 seconds .

        <?php  
        sleep(60*6); 
        echo "test"; 
         die();
        ?>
        

        qt codes

        #include <QCoreApplication>
        #include <QtNetwork/QNetworkAccessManager>
        #include <QtNetwork/QNetworkReply>
        #include <QtNetwork/QNetworkRequest>
        #include <QDebug>
        #include <QObject>
        #include <QEventLoop>
        #include <QDateTime>
        
        void replyFinished(){
            QDateTime dateTime;
            qDebug() << "replyFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        }
        void managerFinished(){
            QDateTime dateTime;
            qDebug() << "managerFinished "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        }
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QNetworkAccessManager manager;
            manager.setTransferTimeout(60*1000 * 7);// 7 minutes timeout
            QNetworkReply *reply;
            QNetworkRequest request;
        
            request.setTransferTimeout(60*1000 * 7);// 7 minutes timeout
            request.setUrl(QUrl(u8"https://www.dajuan.com/home/test"));// 6 minutes return "test"
            request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::NoLessSafeRedirectPolicy);
        
        
            reply = manager.get(request);
        
            QObject::connect(reply,&QNetworkReply::finished,&a,replyFinished);
        
            QDateTime dateTime;
            qDebug() << "reuqest begin " << dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
            QEventLoop eventLoop;
            //更新进度条
            QObject::connect(&manager,&QNetworkAccessManager::finished,&eventLoop,&QEventLoop::quit);
            QObject::connect(&manager,&QNetworkAccessManager::finished,&a,managerFinished);
        
            //block until finish
            eventLoop.exec();
        
            qDebug() << "reuqest done "<< dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        
            if(reply->error() == QNetworkReply::NoError) {
                QByteArray data = reply->readAll();
                QString s = QString::fromStdString(data.toStdString());
                qDebug() << "reply string " << s;
            }
            else{
                QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
                QString msg = QString("code:%1 \ninfo:%2").arg(statusCode.toInt()).arg(reply->errorString());
                qDebug() << "reply error string " << msg;
            }
        
            return a.exec();
        }
        
        

        debug output is like this,we can see that the request is [timeout,after 7 miniutes]

        reuqest begin  "2024-06-25 09:32:15"
        managerFinished  "2024-06-25 09:39:15"
        replyFinished  "2024-06-25 09:39:15"
        reuqest done  "2024-06-25 09:39:15"
        reply error string  "code:0 \ninfo:Operation canceled"
        

        if server return time is shorter than 300s ,debug output is down here ..I performs well.

        reuqest begin  "2024-06-25 09:42:48"
        managerFinished  "2024-06-25 09:43:49"
        replyFinished  "2024-06-25 09:43:49"
        reuqest done  "2024-06-25 09:43:49"
        reply string  "test"
        

        I know that ,QNetworkAccessManager and QNetworkRequest default timeout is 300s.(Also,I don't konw the difference of these two timeout...)
        my qt version is 5.15.2 ,and 5.15.14(i thought it's a bug,so I upgrades to 5.15.14..but, these 2 version is the same result)

        So,this is a bug,or I used this in a wrong way?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 25 Jun 2024, 11:49 last edited by
        #3

        @nelson-W said in network request will always timeout with a long time server return?:

        QEventLoop eventLoop;

        Why a local event loop?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply 25 Jun 2024, 13:04
        0
        • J jsulm
          25 Jun 2024, 11:49

          @nelson-W said in network request will always timeout with a long time server return?:

          QEventLoop eventLoop;

          Why a local event loop?

          N Offline
          N Offline
          nelson.W
          wrote on 25 Jun 2024, 13:04 last edited by
          #4

          @jsulm
          As comment,block until request finished.Synchronous request.

          J 1 Reply Last reply 25 Jun 2024, 15:44
          0
          • N nelson.W
            25 Jun 2024, 13:04

            @jsulm
            As comment,block until request finished.Synchronous request.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 25 Jun 2024, 15:44 last edited by
            #5

            @nelson-W said in network request will always timeout with a long time server return?:

            Synchronous request.

            But do you really need synchronous requests?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nelson.W
              wrote on 1 Jul 2024, 02:51 last edited by
              #6

              Qt network cannot keep alive.I 'm not sure of this,butI didn't find the attribute to set with.
              I change to use lib curl .libcurl perform well.
              Forum admin ,you can end this topic.

              1 Reply Last reply
              0

              2/6

              25 Jun 2024, 02:03

              4 unread
              • Login

              • Login or register to search.
              2 out of 6
              • First post
                2/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved