PDA

View Full Version : QHttp



big4mil
6th October 2008, 19:43
Hi all,

I am quite new to QHttp and now I have my first Problem with a QHttp request.

hier my Code:



void MainWindow::on_pb_getIMDB_clicked()
{
httpRequestAborted = false;

QHttpRequestHeader header("GET", "/find?s=all&q=300&x=15&y=7 HTTP/1.1");
header.setValue("Host", "german.imdb.com");
header.setValue("User-Agent", "Mozilla/5.0 (X11; U; LinuMozx i686; de; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-8.1 Firefox/3.0.1");
header.setValue("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
header.setValue("Accept-Language", "de-ch,de;q=0.8,de-de;q=0.6,en-us;q=0.4,en;q=0.2");
header.setValue("Accept-Encoding", "gzip,deflate");
header.setValue("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
header.setValue("Keep-Alive", "300");
header.setValue("Connection", "keep-alive");

http->setHost( "german.imdb.com" );
httpGetId = http->request( header );
}

void MainWindow::httpRequestFinished( int requestId, bool error )
{
if (requestId != httpGetId)
return;

if ( error )
{
QMessageBox::information( this, tr( "HTTP" ),
tr( "Download failed: %1." )
.arg( http->errorString() ));
}
else
{
QByteArray m_data = http->readAll();

qDebug() << QString::number( m_data.size() );
QString text(m_data.data());
te_test->setText( text );
}
}


The signals and slots are working correctly. The size I receive is around 8298 bytes, but I can not read them. I mean, I should receive some HTML stuff (I think so) but if I try to put them into a file or a text field it does not show me anything.

Do I something wrong? May someone give me an advice please?

Regards
big4mil

yuriry
6th October 2008, 23:15
To copy data to a QString you could probably do something like this:



QTextIStream s(m_data);
QString text = s.readAll();
te_test->setText(text);


But pay attention to the documentation of readAll()


Reads the entire content of the stream, and returns it as a QString. Avoid this function when working on large files, as it will consume a significant amount of memory.