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
    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!!!

  2. #2
    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.

  3. #3
    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 ????

  4. #4
    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.