Checksum mismatch on uploading Files using QHttpMultipart



  • I am using QHttpMultiPart to upload a file to a server using a rest api. The checksum of the uploaded file and local file are different. Below is the Qt code.

        QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
        QHttpPart macPart;
        macPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"mac\""));
        macPart.setBody(macID.toLatin1());
    
        QHttpPart certPart;
        certPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
        certPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"cert\"; filename=\"cert\"\r\n"));
        QFile fp(certFilePath);
        if(!fp.open(QIODevice::ReadOnly))
        {
            return -1;
        }
        certPart.setBodyDevice(&fp);
        fp.setParent(&multiPart);
    
        multiPart.append(macPart);
        multiPart.append(certPart);
    
        FREE_MEMORY(m_networkReply);
    
        QNetworkRequest request(requestUrl);
        m_networkReply = m_networkManager->post(request, &multiPart);
    
        connect(m_networkReply, SIGNAL(uploadProgress(qint64,qint64)),this,SIGNAL(uploadProgress(qint64,qint64)));
        connect(m_networkReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onError(QNetworkReply::NetworkError)));
        connect(m_networkReply, SIGNAL(finished()),this, SLOT(uploadFinished()));
    
        m_progressDialog->exec();
    
        QByteArray data = m_networkReply->readAll();
    

    When I upload using website, the files checksum are same.



  • I can't say about the code, but an obvious way to find out where the problem might be is to compare the two files byte by byte. Checksum may every byte you feed to it, including all end-of bytes and characters. I once saved my employer who couldn't get checksum calculation working by noticing that in a C program \0 character (end of string) was checked, too, and that's not obvious because you don't usually add it manually with a string literal.

    So if you have actually two files, check them with a hex editor or something like that. Tools, commands and functions that print strings may give false results because they may hide some bytes.



  • I checked them using hex editor. Lot of bytes are different. I doubt it is something to do with encoding. Maybe QNetworkAccessManger is changing encoding while uploading file. My client is running on Linux machine but the server is on windows.



  • I compared requests sent from Qt and webpage using wireshark.

    Request from webpage.

    Frame 155: 142 bytes on wire (1136 bits), 142 bytes captured (1136 bits) on interface 0
    Ethernet II, Src: HonHaiPr_a5:fb:7b (44:1c:a8:a5:fb:7b), Dst: HewlettP_78:0b:51 (1c:c1:de:78:0b:51)
    Internet Protocol Version 4, Src: 192.168.0.152, Dst: 192.168.0.5
    Transmission Control Protocol, Src Port: 51002 (51002), Dst Port: 80 (80), Seq: 4345, Ack: 1, Len: 76
    [4 Reassembled TCP Segments (4420 bytes): #152(1448), #153(1448), #154(1448), #155(76)]
    Hypertext Transfer Protocol
        POST /activation HTTP/1.1\r\n
            [Expert Info (Chat/Sequence): POST /activation HTTP/1.1\r\n]
                [POST /activation HTTP/1.1\r\n]
                [Severity level: Chat]
                [Group: Sequence]
            Request Method: POST
            Request URI: /activation
            Request Version: HTTP/1.1
        HOST: 192.168.0.5\r\n
        content-type: multipart/form-data; boundary=----WebKitFormBoundaryAztTMEvqCVlT660I\r\n
        content-length: 4266\r\n
            [Content length: 4266]
        \r\n
        [Full request URI: http://192.168.0.5/activation]
        [HTTP request 1/1]
        [Response in frame: 158]
    MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryAztTMEvqCVlT660I"
        [Type: multipart/form-data]
        First boundary: ------WebKitFormBoundaryAztTMEvqCVlT660I\r\n
        Encapsulated multipart part:  (application/pkix-cert)
            Content-Disposition: form-data; name="cert"; filename="1200_syspubkey.cert"\r\n
            Content-Type: application/pkix-cert\r\n\r\n
            PKIX CERT File Format
                Certificate 0 items
                    BER Error: Wrong field in SEQUENCE  expected class:UNIVERSAL(0) tag:16(SEQUENCE) but found class:APPLICATION(1) tag:1
                        [Expert Info (Warn/Malformed): BER Error: Wrong field in SEQUENCE]
                            [BER Error: Wrong field in SEQUENCE]
                            [Severity level: Warn]
                            [Group: Malformed]
        Boundary: \r\n------WebKitFormBoundaryAztTMEvqCVlT660I\r\n
        Encapsulated multipart part: 
            Content-Disposition: form-data; name="mac"\r\n\r\n
            Data (12 bytes)
                Data: 303031453637313933393537
                [Length: 12]
        Last boundary: \r\n------WebKitFormBoundaryAztTMEvqCVlT660I--\r\n
    

    Request from Qt

    Frame 102: 247 bytes on wire (1976 bits), 247 bytes captured (1976 bits) on interface 0
    Ethernet II, Src: HonHaiPr_a5:fb:7b (44:1c:a8:a5:fb:7b), Dst: HewlettP_78:0b:51 (1c:c1:de:78:0b:51)
    Internet Protocol Version 4, Src: 192.168.0.152, Dst: 192.168.0.5
    Transmission Control Protocol, Src Port: 51016 (51016), Dst Port: 80 (80), Seq: 4828, Ack: 1, Len: 181
    [5 Reassembled TCP Segments (5008 bytes): #97(483), #98(1448), #99(1448), #100(1448), #102(181)]
    Hypertext Transfer Protocol
        POST /activation HTTP/1.1\r\n
            [Expert Info (Chat/Sequence): POST /activation HTTP/1.1\r\n]
                [POST /activation HTTP/1.1\r\n]
                [Severity level: Chat]
                [Group: Sequence]
            Request Method: POST
            Request URI: /activation
            Request Version: HTTP/1.1
        Content-Type: multipart/form-data; boundary=boundary_.oOo._MjE0Mzg1MTQyOQ==MTEzNzE1NDM2NQ==MTQ5ODY2MTAyNg==\r\n
        accept: application/json\r\n
        accept-encoding: gzip, deflate\r\n
        accept-language: en-US,en;q=0.8\r\n
        Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n
        user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36\r\n
        MIME-Version: 1.0\r\n
        Content-Length: 4525\r\n
            [Content length: 4525]
        Connection: Keep-Alive\r\n
        Host: 192.168.0.5:80\r\n
        \r\n
        [Full request URI: http://192.168.0.5:80/activation]
        [HTTP request 1/1]
        [Response in frame: 105]
    MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "boundary_.oOo._MjE0Mzg1MTQyOQ==MTEzNzE1NDM2NQ==MTQ5ODY2MTAyNg=="
        [Type: multipart/form-data]
        First boundary: --boundary_.oOo._MjE0Mzg1MTQyOQ==MTEzNzE1NDM2NQ==MTQ5ODY2MTAyNg==\r\n
        Encapsulated multipart part:  (text/plain)
            Content-Type: text/plain;boundary=boundary_.oOo._MjE0Mzg1MTQyOQ==MTEzNzE1NDM2NQ==MTQ5ODY2MTAyNg==\r\n
            Content-Disposition: form-data; name="mac"\r\n\r\n
            Line-based text data: text/plain
                001E67193957
        Boundary: \r\n--boundary_.oOo._MjE0Mzg1MTQyOQ==MTEzNzE1NDM2NQ==MTQ5ODY2MTAyNg==\r\n
        Encapsulated multipart part: 
            Content-Disposition: multipart/form-data; name="cert"; filename="1200_syspubkey.cert"\r\n\r\n
            Data (4072 bytes)
                Data: 436f6e74656e742d547970653a206170706c69636174696f...
                [Length: 4072]
        Last boundary: \r\n--boundary_.oOo._MjE0Mzg1MTQyOQ==MTEzNzE1NDM2NQ==MTQ5ODY2MTAyNg==--\r\n
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.