PDA

View Full Version : How to separate http response headers and contents of downloaded file?



Ali Reza
4th June 2012, 08:08
Hi guys !!
a problem
plz help me :-(
How to separate http response headers and contents of downloaded file?

best regards
bye

StrikeByte
4th June 2012, 13:31
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 (http://www.jmarshall.com/easy/http/#structure)

ChrisW67
5th June 2012, 00:23
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.

Ali Reza
5th June 2012, 06:23
@ ChrisW67
i am using a QTcpSocket for my connection.

10Q

StrikeByte
5th June 2012, 07:55
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

gowreesh
1st May 2015, 08:15
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!!!

jefftee
1st May 2015, 08:22
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.

gowreesh
4th May 2015, 01:07
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 ????

jefftee
4th May 2015, 02:16
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().