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. QNetworkReply Just download the head of the Url file
Qt 6.11 is out! See what's new in the release blog

QNetworkReply Just download the head of the Url file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.2k 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.
  • A Offline
    A Offline
    An other french
    wrote on last edited by An other french
    #1

    Hello,
    See below a part of the code which i want to make advanced

    void Form::testNum (QString num)
    {
        qDebug() << num;
    
        manager = new QNetworkAccessManager(this);
    
        QNetworkRequest request;
        request.setUrl(QUrl("http://www.xxxxxx.com/"+num));
    
        response = manager->get(request);
    
        connect(response, &QNetworkReply::finished,this,&Form::getResult );
    }
    
    void Form::getResult() {
        qDebug() << "Finished";
        qDebug() << response->readAll();
        response->deleteLater();
        response = Q_NULLPTR;
    }
    
    

    I saw the way to store it in a QByteArray to make an analysis.
    But do you think is possible to get a part of the html code like the <head> without downloading all the file?

    Thanks for reading.
    Bye.

    raven-worxR 1 Reply Last reply
    0
    • A Offline
      A Offline
      An other french
      wrote on last edited by
      #2
      void Form::getResult() {
          qDebug() << "Finished";
          //qDebug() << response->readAll();
          QByteArray output = response->readAll();
          QString tmp = output;
          QStringList list;
          list = tmp.split(QRegExp("<head>"));
          tmp=list[1];
          list=tmp.split("</head>");
          tmp=list[0];
          qDebug() << "Head is : " + tmp;
      
          response->deleteLater();
          response = Q_NULLPTR;
      }
      

      Easy to write but it's not my point of view.

      1 Reply Last reply
      0
      • A An other french

        Hello,
        See below a part of the code which i want to make advanced

        void Form::testNum (QString num)
        {
            qDebug() << num;
        
            manager = new QNetworkAccessManager(this);
        
            QNetworkRequest request;
            request.setUrl(QUrl("http://www.xxxxxx.com/"+num));
        
            response = manager->get(request);
        
            connect(response, &QNetworkReply::finished,this,&Form::getResult );
        }
        
        void Form::getResult() {
            qDebug() << "Finished";
            qDebug() << response->readAll();
            response->deleteLater();
            response = Q_NULLPTR;
        }
        
        

        I saw the way to store it in a QByteArray to make an analysis.
        But do you think is possible to get a part of the html code like the <head> without downloading all the file?

        Thanks for reading.
        Bye.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @An-other-french said in QNetworkReply Just download the head of the Url file:

        But do you think is possible to get a part of the html code like the <head> without downloading all the file?

        your example still downloads the whole file.

        Just downloading selectively a part of a HTML content isn't possible.
        Instead of connecting to the QNetworkReply::finished signal you can connect to the readyRead signal and save the response's data in a buffer until you find </head>.

        void Form::getResult() {
            m_Buffer += response->readAll();
            
           int found = m_Buffer.indexOf("</head>");
            if( found >= 0 )
           {
                 response->deleteLater(); // implicitly aborts the request if not yet finished
                 response = Q_NULLPTR;
           }
        }
        

        But also note, that not each HTML page is required to have a <head> tag.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • A Offline
          A Offline
          An other french
          wrote on last edited by An other french
          #4

          Thanks.
          Changing the signal is smart.

          In another way, i look for a protocol like "robot" which just catching the summary of the internet page.
          It'll be construct with the metadatas built with the <head> of the page ( get_meta_tags in php ).

          Perhaps this.

          raven-worxR 1 Reply Last reply
          0
          • A An other french

            Thanks.
            Changing the signal is smart.

            In another way, i look for a protocol like "robot" which just catching the summary of the internet page.
            It'll be construct with the metadatas built with the <head> of the page ( get_meta_tags in php ).

            Perhaps this.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @An-other-french said in QNetworkReply Just download the head of the Url file:

            Perhaps this.

            but those are HTTP headers and have nothing todo with HTML headers.
            If you are just interested in the HTTP-Headers then use QNetworkAccessManager::head() (instead of get()) and the content-body is not transferred.

            HTML-headers are contained in the data-body und thus need to be requested via get() - and be read with the approach i've posted.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            4

            • Login

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