chamoda
17th January 2013, 06:16
Hi!
I'm a beginner to Network programming and I'm trying to write a simple console application which return html source from server.
this is the code
void Downloader::doDownload()
{
QHttp *http = new QHttp(this);
http->setHost("c-studios.host56.com");
http->get("/index.html");
connect(http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader) ), this, SLOT(responseHeaderReceived(QHttpResponseHeader))) ;
connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
connect(http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(showHtml()));
}
// ....
void Downloader::responseHeaderReceived(const QHttpResponseHeader &resp)
{
qDebug() << "Size: " << resp.contentLength(); //works fine. show correct length
qDebug() << "Type: " << resp.contentType(); // works fine
qDebug() << "Status: " << resp.statusCode(); // fine return 200
}
void Downloader::requestFinished(int id, bool error)
{
if(error)
{
qDebug() << "Error!";
}
else
{
qDebug() << "Ok!";
}
}
void Downloader::showHtml()
{
QString html(http->readAll()); //this is the problem. QString is empty
qDebug() << html;
}
when i call http->readAll() it's return empty byte array. I can't fugure out what is the problem. someone help please. thanks
I'm a beginner to Network programming and I'm trying to write a simple console application which return html source from server.
this is the code
void Downloader::doDownload()
{
QHttp *http = new QHttp(this);
http->setHost("c-studios.host56.com");
http->get("/index.html");
connect(http, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
connect(http, SIGNAL(responseHeaderReceived(QHttpResponseHeader) ), this, SLOT(responseHeaderReceived(QHttpResponseHeader))) ;
connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));
connect(http, SIGNAL(readyRead(QHttpResponseHeader)), this, SLOT(showHtml()));
}
// ....
void Downloader::responseHeaderReceived(const QHttpResponseHeader &resp)
{
qDebug() << "Size: " << resp.contentLength(); //works fine. show correct length
qDebug() << "Type: " << resp.contentType(); // works fine
qDebug() << "Status: " << resp.statusCode(); // fine return 200
}
void Downloader::requestFinished(int id, bool error)
{
if(error)
{
qDebug() << "Error!";
}
else
{
qDebug() << "Ok!";
}
}
void Downloader::showHtml()
{
QString html(http->readAll()); //this is the problem. QString is empty
qDebug() << html;
}
when i call http->readAll() it's return empty byte array. I can't fugure out what is the problem. someone help please. thanks