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. Howto neglect these bytes while downloading file from http server
QtWS25 Last Chance

Howto neglect these bytes while downloading file from http server

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 673 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by
    #1

    I'm downloading binary file(s) from a python http server.
    The downloading is fine but I get 4 extra bytes appended to my binary file every time I receive a file. I believe these bytes represents time. Although, I can simply strip these bytes within my program but what if in future I use this program with another server and that server transfer some more/less bytes along with the requested file (?)

    Is their a generic way of how to get only the requested file bytes ?

    Here is how I'm receiving file

    QNetworkRequest request;
        request.setUrl(QUrl("http://link/to/file"));
    
    void HTTP_Class::GetReq(QNetworkRequest &request){
    
        connect(&manager, &QNetworkAccessManager::finished, this, &HTTP_Class::replyFinished);
        reply = manager.get(request);
    
    }
    
    void HTTP_Class::replyFinished(){
    
        qDebug() << "replyFinished is called";
        QByteArray bytes;
    
        if (reply->error() == QNetworkReply::NoError){
            bytes = reply->readAll();
        }
        QFile file("path/to/file");
        if (!file.open(QIODevice::WriteOnly))
            return;
    
        QDataStream out(&file);
        out << bytes;
        file.close();
    }
    

    Here is an example which shows extra bytes along with the text "testing"

    00 00 00 07 74 65 73 74 69 6E 67 // testing

    jsulmJ 1 Reply Last reply
    0
    • P pingal

      I'm downloading binary file(s) from a python http server.
      The downloading is fine but I get 4 extra bytes appended to my binary file every time I receive a file. I believe these bytes represents time. Although, I can simply strip these bytes within my program but what if in future I use this program with another server and that server transfer some more/less bytes along with the requested file (?)

      Is their a generic way of how to get only the requested file bytes ?

      Here is how I'm receiving file

      QNetworkRequest request;
          request.setUrl(QUrl("http://link/to/file"));
      
      void HTTP_Class::GetReq(QNetworkRequest &request){
      
          connect(&manager, &QNetworkAccessManager::finished, this, &HTTP_Class::replyFinished);
          reply = manager.get(request);
      
      }
      
      void HTTP_Class::replyFinished(){
      
          qDebug() << "replyFinished is called";
          QByteArray bytes;
      
          if (reply->error() == QNetworkReply::NoError){
              bytes = reply->readAll();
          }
          QFile file("path/to/file");
          if (!file.open(QIODevice::WriteOnly))
              return;
      
          QDataStream out(&file);
          out << bytes;
          file.close();
      }
      

      Here is an example which shows extra bytes along with the text "testing"

      00 00 00 07 74 65 73 74 69 6E 67 // testing

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @pingal said in Howto neglect these bytes while downloading file from http server:

      Is their a generic way of how to get only the requested file bytes ?

      You need to know the format of the data you're downloading. Qt can't know what the data you're downloading mean. So clarify the format and handle it accordingly.

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

      1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        The four bytes look like the number of bytes in the remainder of the message.

        1 Reply Last reply
        1
        • P Offline
          P Offline
          pingal
          wrote on last edited by
          #4

          ChrisW67: You are right. That's the data length. The problem was in the below lines:

          QDataStream out(&file);
          out << bytes;
          

          When bytes are written to QDataStream in this fashion, Its write the content length followed by the actual data. (I thought it will just write the content).

          So i figured out to use QDataStream::writeRawData

          QDataStream out(&file);
           size_t size = bytes.size();
           out.writeRawData(bytes, size);
           file.close();
          
          C 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Why do you need a QDataStream then at all?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • P Offline
              P Offline
              pingal
              wrote on last edited by
              #6

              I don't actually need it if i just use it for downloading a file, In fact, it is easier to just use QFile::write(..) in the above case. I though it would be easier to prefix/suffix files content like below:

              out << prefix;
              out << file;
              out << suffix

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                If you use QDataStream on the sender side you must also use it on the receiver side. Everything else will break sooner or later.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                3
                • P Offline
                  P Offline
                  pingal
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher : I didn't notice that, can you please elaborate it a little.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Don't know what else I should say - you use QDataStream to create your stream so you have to use QDataStream to deserialize the data later on. QDataStream adds data to the stream (as you already noticed) and may alter your data in any way: "A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris."
                    So reading it as raw data may or may not work. The format of QDataStream's binary representation is not documented so you it can change.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • P Offline
                      P Offline
                      pingal
                      wrote on last edited by
                      #10

                      oky. thank you :)

                      1 Reply Last reply
                      0
                      • P pingal

                        ChrisW67: You are right. That's the data length. The problem was in the below lines:

                        QDataStream out(&file);
                        out << bytes;
                        

                        When bytes are written to QDataStream in this fashion, Its write the content length followed by the actual data. (I thought it will just write the content).

                        So i figured out to use QDataStream::writeRawData

                        QDataStream out(&file);
                         size_t size = bytes.size();
                         out.writeRawData(bytes, size);
                         file.close();
                        
                        C Offline
                        C Offline
                        ChrisW67
                        wrote on last edited by
                        #11

                        @pingal I missed that in your original post. I assumed you were receiving extra bytes, which is not the case.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          pingal
                          wrote on last edited by
                          #12

                          @ChrisW67 Yes, that's what i thought at first but when I STDOUT QByteArray, then i realized that serializing data to stream is causing those extra bytes.

                          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