Results 1 to 9 of 9

Thread: How to separate http response headers and contents of downloaded file?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Unhappy How to separate http response headers and contents of downloaded file?

    Hi guys !!
    a problem
    plz help me :-(
    How to separate http response headers and contents of downloaded file?

    best regards
    bye

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to separate http response headers and contents of downloaded file?

    If i can remember correct, the header and body are separated by an empty line (only containing CRLF (char 13 and 10))
    Best way to check is to google for the HTTP protocol or take a look at this site

  3. The following user says thank you to StrikeByte for this useful post:

    Ali Reza (5th June 2012)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to separate http response headers and contents of downloaded file?

    That will depend on how you download them. If you are using QNetworkAccessManager then the response headers and the data payload are separated for you in the QNetworkReply.

  5. The following user says thank you to ChrisW67 for this useful post:

    Ali Reza (5th June 2012)

  6. #4
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to separate http response headers and contents of downloaded file?

    @ ChrisW67
    i am using a QTcpSocket for my connection.

    10Q

  7. #5
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to separate http response headers and contents of downloaded file?

    Quote Originally Posted by ChrisW67 View Post
    That will depend on how you download them. If you are using QNetworkAccessManager then the response headers and the data payload are separated for you in the QNetworkReply.
    Cool I didn't know qt had a class for it. Thx

  8. #6
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to separate http response headers and contents of downloaded file?

    Hi,
    am also facing the same issue . I want to separate the header and body . and am extracted the xml file using QNetworkReply only.
    Anyone please help me by showing how to separate the header and body!!!

  9. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to separate http response headers and contents of downloaded file?

    The headers are *already* separated from the response data when using QNetworkReply. You retrieve the body or response data by the QNetworkReply::read* methods or incrementally by connecting a slot to QNetworkReply::readyRead.

    The headers are available via the QNetworkReply::rawHeader* methods.

  10. #8
    Join Date
    May 2015
    Posts
    26
    Thanks
    11
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to separate http response headers and contents of downloaded file?

    Hi,
    Thanks for the reply.
    THis is my first project as a developer. Requesting you for the one more clarification.

    Here is the question.
    By using this below 1st piece of code I can read both header and body of the xml file.
    1. QString data = (QString) pReply->readAll();

    and by using this 2nd piece of code I can read only body of the xml file
    2. QString data = (QString) pReply->readRead();

    Does this my understanding this concept is correct.?

    Please Find the below piece of code I have written for to pass the xml using http . my intention is to pass only the received xml
    body to the function decoder .

    void Uploader::upload(QString xmlFilePath)
    {
    qDebug() << "Upload Starting";
    QFileInfo fileInfo(xmlFilePath);

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart filePart;
    filePart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("HTTP/1.1 200 OK"));
    filePart.setHeader(QNetworkRequest::ContentTypeHea der, QVariant("text/xml; charset=utf-8"));

    QFile *file = new QFile(xmlFilePath);
    if ( !file->exists() )
    {
    qDebug() << "File Does not exist";
    }

    file->open(QIODevice::ReadOnly);
    filePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    multiPart->append(filePart);

    QUrl url("http://google.com");
    QNetworkRequest request(url);

    pManager = new QNetworkAccessManager();

    pReply = pManager->post(request, multiPart);
    multiPart->setParent(pReply);

    QObject::connect(pReply, SIGNAL(QNetworkReply::finished()),this, SLOT(Uploader::uploadFinished()));
    uploadInProgress = true;
    uploadtoDecoder();
    }

    /**
    * @brief Uploader::uploadFinished
    */
    void Uploader::uploadFinished()
    {
    QString data = (QString) pReply->readAll();
    qDebug() << data;
    qDebug() << "Upload finished";

    uploadInProgress = false;
    if ( pReply->error() > 0 )
    {
    qDebug() << "Error occured: " << pReply->error() << " : " << pReply->errorString();
    }
    else
    {
    qDebug() << "Upload success";
    }
    pReply->deleteLater();

    }


    /**
    * @brief Uploader::uploadToDecoder
    */

    void Uploader::uploadToDecoder()
    {
    /* I have doubt here only */
    QString datafull = (QString) pReply->readAll(); \\for to read both header and body
    qDebug() << data;
    QString data = (QString) pReply->readRead(); \\for to read only body
    qDebug() << data;

    decoder(data); // here i am passing the xml body only
    .
    .
    .
    .
    .


    }



    Does my understanding is correct ????

  11. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to separate http response headers and contents of downloaded file?

    Quote Originally Posted by gowreesh View Post
    Hi,
    Thanks for the reply.
    THis is my first project as a developer. Requesting you for the one more clarification.

    Here is the question.
    By using this below 1st piece of code I can read both header and body of the xml file.
    1. QString data = (QString) pReply->readAll();

    and by using this 2nd piece of code I can read only body of the xml file
    2. QString data = (QString) pReply->readRead();

    Does this my understanding this concept is correct.?
    In my experience with QNetworkReply, QNetworkReply::readAll() does not return header data. You'd have to show me output that contains headers and data for me to believe that...

    Your 2nd example isn't even valid syntax, so no comment there, sorry. Show me where you receive headers *and* data when doing a readAll().

Similar Threads

  1. Replies: 9
    Last Post: 10th October 2012, 22:55
  2. QTcpSocket : gettin all the response from HTTP
    By giusepped in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2011, 12:23
  3. CRLF in HTTP response
    By giusepped in forum Qt Programming
    Replies: 22
    Last Post: 22nd September 2011, 07:24
  4. How to parse xml from a http xml response?
    By dineshkumar in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2011, 07:28
  5. QHTTP does not see tunneled TCP HTTP OK authentication response
    By SailingDreams in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2009, 09:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.