×

Discussion Board

Results 1 to 7 of 7

Threaded View

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Download with HTTP crashes files - how to fix it?

    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:
    1. Is it at server problem (do not think so)?
    2. Is it impossible to use this code to download archives (seems to work with "text" files like .txt or .php)?
    3. 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.
    Last edited by _DICE_; 2011-12-30 at 11:09.

Similar Threads

  1. Download multiple files using http
    By kuddy_abc in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2010-05-28, 05:08
  2. Replies: 3
    Last Post: 2009-04-09, 15:40
  3. Replies: 0
    Last Post: 2003-07-24, 10:46
  4. Upload and Download files using Wap instead of HTTP
    By arizkohli in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2003-07-19, 22:19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×