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. qt QNetworkAccessManager can not get any html
Forum Updated to NodeBB v4.3 + New Features

qt QNetworkAccessManager can not get any html

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.0k Views 2 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.
  • S Offline
    S Offline
    seamanmei
    wrote on last edited by p3c0
    #1

    we use qt QNetworkAccessManager request a remote http url has problme,can not get any html,but can get the responese body size.this code:

       manager=new QNetworkAccessManager(0);
        cookie = new QNetworkCookieJar(0);
        manager->setCookieJar(cookie);
        QString str_url="http://www.wangdaizhijia.com/daohang.html";
        QNetworkRequest request;
        request.setUrl(QUrl(str_url));
        request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        request.setRawHeader("Accept-Language", "zh-CN,zh;q=0.8");
        request.setRawHeader("Cache-Control", "no-cache");
        request.setRawHeader("Connection", "keep-alive");
        request.setRawHeader("DNT","1");
        request.setRawHeader("Pragma","no-cache");
        request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36");
    QNetworkReply *reply=manager->get(request);
        QEventLoop eventLoop;
        QTimer timer;
        QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit()));
        QObject::connect(&timer, SIGNAL(timeout()),&eventLoop, SLOT(quit()));
        timer.start(5*1000);
        eventLoop.exec();
        if(!timer.isActive())
        {
            timer.stop();
        }
        QByteArray temp=reply->readAll();
        qDebug()<<"size is"<<temp.size();
        qDebug()<<"html is"<<temp;
    

    but if request other url e.g. http://www.google.com ,it's work,no problem,why? please help me,thanks.

    Edited: Please put code after ``` (3 backticks) and end with the same - p3c0

    p3c0P 1 Reply Last reply
    0
    • S seamanmei

      we use qt QNetworkAccessManager request a remote http url has problme,can not get any html,but can get the responese body size.this code:

         manager=new QNetworkAccessManager(0);
          cookie = new QNetworkCookieJar(0);
          manager->setCookieJar(cookie);
          QString str_url="http://www.wangdaizhijia.com/daohang.html";
          QNetworkRequest request;
          request.setUrl(QUrl(str_url));
          request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
          request.setRawHeader("Accept-Language", "zh-CN,zh;q=0.8");
          request.setRawHeader("Cache-Control", "no-cache");
          request.setRawHeader("Connection", "keep-alive");
          request.setRawHeader("DNT","1");
          request.setRawHeader("Pragma","no-cache");
          request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36");
      QNetworkReply *reply=manager->get(request);
          QEventLoop eventLoop;
          QTimer timer;
          QObject::connect(reply, SIGNAL(finished()),&eventLoop, SLOT(quit()));
          QObject::connect(&timer, SIGNAL(timeout()),&eventLoop, SLOT(quit()));
          timer.start(5*1000);
          eventLoop.exec();
          if(!timer.isActive())
          {
              timer.stop();
          }
          QByteArray temp=reply->readAll();
          qDebug()<<"size is"<<temp.size();
          qDebug()<<"html is"<<temp;
      

      but if request other url e.g. http://www.google.com ,it's work,no problem,why? please help me,thanks.

      Edited: Please put code after ``` (3 backticks) and end with the same - p3c0

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @seamanmei The reply must be taking more time and by the time it comes your timer already kills the event loop. Try setting a greater timeout than current one. In case of google it works, as it first just returns a redirect info page which is usually fast.
      AFAIK the recommended way would be not to use your own eventloop and just rely on the QNetworkAccessManager's signals and your own slots. In this case you can just connect QNetworkAccessManager's finished(QNetworkReply*) signal to your own slot where there you can read the data using QNetworkReply's readAll().

      157

      S 1 Reply Last reply
      0
      • p3c0P p3c0

        @seamanmei The reply must be taking more time and by the time it comes your timer already kills the event loop. Try setting a greater timeout than current one. In case of google it works, as it first just returns a redirect info page which is usually fast.
        AFAIK the recommended way would be not to use your own eventloop and just rely on the QNetworkAccessManager's signals and your own slots. In this case you can just connect QNetworkAccessManager's finished(QNetworkReply*) signal to your own slot where there you can read the data using QNetworkReply's readAll().

        S Offline
        S Offline
        seamanmei
        wrote on last edited by
        #3

        @p3c0 thank you answer my question,im try

        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