Important: Please read the Qt Code of Conduct -

[SOLVED] How to download files from SourceForge using Qt

  • I'm writing a C++ application using Qt framework that needs to download a file from SourceForge.

    I have to download this file

    I wrote the following code using Qt and the QHttp class:

    @QFile tmpfile;
    int hostreqid;
    int checkreqid;
    hostreqid = http.setHost("",QHttp::ConnectionModeHttp);
    checkreqid = http.get(QString(QUrl::toPercentEncoding("/projects/meshlab/files/updates/1.3.3/updates.xml")),&tmpfile);
    void parseAnswer( int id,bool error )
    if (!error && (id == checkreqid))
    if (error)
    QHttp::Error err = http.error();
    QString errstrg = http.errorString();
    Both QHttp::setHost and QHttp::get are not blocking functions returning immediately an int id. When the http file transfer completed the parseAnswer function is automatically called. The problem is that inside the updates.xml file I got, instead the data I was expecting I received an html file from SouceForge reporting an "Invalid Project" error.

    I noticed that when I access the from browser I have been redirected to page. I tried also this other address but nothing changed.

    Please, notice that I'm using Http protocol (QHttp::ConnectionModeHttp) instead of the https. If I could I would wish to avoid to use the https. Can be the source of the problem?

    Thanks a lot!

  • You can't download from sourceforge directly, use one of it's mirrors, or generate url to be directly redirected to the right mirror:
    QUrl url(""+QString::number(QDateTime::currentDateTime ().toTime_t())+"&use_mirror=heanet");

    heanet is the example mirror in this case.

  • Thanks a lot for your suggestion! Unfortunately now I'm getting an empty file...Have you some other idea? Really really thanks for your help

  • Make sure you handle the HTTP redirect and initiate another request with the location reported back.

  • This works for me:

    #ifndef SFORGE_H
    #define SFORGE_H

    #include <QNetworkAccessManager>
    #include <QNetworkReply>

    class SForge : public QNetworkAccessManager
    explicit SForge(QObject *parent = 0);

    private slots:
    void fin(QNetworkReply *);


    #endif // SFORGE_H

    #include "sforge.h"
    #include <QDateTime>
    #include <QUrl>
    #include <QNetworkRequest>
    #include <QDebug>

    SForge::SForge(QObject *parent):QNetworkAccessManager(parent)
    QUrl url(""+QString::number(QDateTime::currentDateTime().toTime_t())+"&use_mirror=heanet");

    connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(fin(QNetworkReply*)));
    QNetworkRequest req(url);


    void SForge::fin(QNetworkReply * reply)
    if(reply->error() != QNetworkReply::NoError)
    qDebug() << reply->errorString();

    if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 307 || reply->rawHeaderList().contains("Location"))
        QNetworkRequest req(reply->header(QNetworkRequest::LocationHeader).toString());
    qDebug() << __LINE__ << reply->bytesAvailable() << reply->readAll();


  • I love you! You own my soul! Probably my problem was (also like Terence suggested) that I didn't manage the redirect... Thanks a lot! Really Really thanks!

  • I'm glad I could help you! :) Please add "[SOLVED]" prefix, left to the topic subject. Thanks!

Log in to reply