Hi,
I try to download some files, mostly archives (*.7z, *.rar), using HTTP.
My code for it is:
Code:
void downloader::Do_Download()
{
http = new QHttp(this);
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)));
QString host = "***"; //
qDebug() << "Host: " + host;
QString dl = "***";
qDebug() << "Datei: " + dl;
http->setHost(host);
http->get(dl);
}
void downloader::stateChanged ( int state )
{
switch (state)
{
case 0:
qDebug() << "Unconnected";
break;
case 1:
qDebug() << "Hhost Lookup";
break;
case 2:
qDebug() << "Connecting";
break;
case 3:
qDebug() << "Sending";
break;
case 4:
qDebug() << "Reading";
break;
case 5:
qDebug() << "Connect";
break;
case 6:
qDebug() << "Close";
break;
}
}
void downloader::responseHeaderReceived ( const QHttpResponseHeader & resp )
{
qDebug() << "Size" << resp.contentLength();
qDebug() << "Type" << resp.contentType();
qDebug() << "Status" << resp.statusCode();
qDebug() << "Content" << resp.toString();
QFile file("C:/html-test/resp.txt");
if (file.open(QIODevice::WriteOnly))
{
QTextStream stream(&file);
stream << resp.toString();
file.close();
}
}
void downloader::requestFinished ( int id, bool error )
{
if (error)
{
qDebug() << "ERROR";
}
else
{
qDebug() << "OK";
QFile file("C:/html-test/test.rar");
if (file.open(QIODevice::WriteOnly))
{
QTextStream stream(&file);
stream << http->readAll();
file.close();
}
}
}
It does download the archives, but it crashes the files. WinRAR reports that the file headers are corrupt and 7Zip reports that the hole archive is corrupt.
So I downloaded some archives using download managers contained in Webbrowsers like Firefox and Opera. I compared my "corrupted" archives with them from the browsers and found out:
The archives are really corrupt, there are differences at some bytes...
The questions are:
- Is it at server problem (do not think so)?
- Is it impossible to use this code to download archives (seems to work with "text" files like .txt or .php)?
- If it is possible: What do I have to change to get it working?
Maybe these are dumb questions but downloads are a absolutely new topic for me.