Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Downloading zip using QNetworkAccessManager



  • Hello, I have a code that downloades a zip with the QNetworkAccessManager.
    The code works fine on my machine, but on another machine the bytesTotal is only 44kb, but the file is 15mb.
    Also on my machine the content-length header returns 15mb, but on his machine the messagebox showing the content-lenght is empty so the content-length is not received.

    I tried messing around with setting different rawHeader options, like accept-encoding gzip, identity etc.
    For me it works even without the rawHeader but on the other machine I can't get it to work.

    
    void Installer::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
    {
        if(!content_length)
        {
            content_length = bytesTotal;
            emit setDownloadSize(bytesTotal);
        }
                char bytes[256]="";
                sprintf(bytes, "%s %d(%d)\n%f MB/s", package, (DWORD)bytesReceived, (DWORD)bytesTotal, (double)((double)bytesReceived/1000)/(double)((double)timer.elapsed()));
    
                emit downloadProgress(bytesReceived);
                emit setDownloadInfo(bytes);
    
    }
    bool showMessage=true;
    
    void Installer::onNewDataDownloaded()
    {
        auto reply = qobject_cast<QNetworkReply*>(sender());
            if (!reply)
                return;
            QByteArray arr = reply->readAll();
            char* data = arr.data();
            DWORD count = arr.count();
            if(showMessage)
            {
                showMessage=false;
                MessageBoxA(0, reply->rawHeader("content-length"), reply->rawHeader("content-length"), 0);
            }
    
    
            body.insert(body.end(), data, data+count);
    }
    
    void Installer::DownloadFileFromHTTP(char* url)
    {
    if(!downloading)
        {
            showMessage=true;
            content_length=0;
    
            MessageBoxA(0, url, "Download URL", 0);
            request = QNetworkRequest((QUrl(url)));
            request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); // HTTPS
            request.setMaximumRedirectsAllowed(5);
            request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
            request.setRawHeader("Accept-Encoding", "identity");
            request.setRawHeader("Accept", "*/*");
            //request.setRawHeader("Transfer-Encoding", "identity");
            request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
    
            QNetworkReply * reply = download_thread.get(request);
            timer.start();
            connect(reply, SIGNAL(readyRead()), this, SLOT(onNewDataDownloaded()));
            connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(onDownloadProgress(qint64, qint64)));
            connect(reply, SIGNAL(finished()), this, SLOT(onFinishedDownloading()), Qt::UniqueConnection);
            if (!reply)
            {
                MessageBoxA(0, "No reply", "", 0);
            }
    }
    }
    

  • Lifetime Qt Champion

    @Vadru said in Downloading zip using QNetworkAccessManager:

    MessageBoxA

    wtf??
    Don't mix WinAPI with Qt when not needed. Also the code is incomplete (e.g. what should be 'download_thread') and passing a char* pointer to a stack around in a signal is ... strange and error prone.


Log in to reply