Search code examples
c++qtsourceforge

QT make app to check if there is an update on sourceforge.net


I want my QT app to check if there is an update on sourceforge.net. For this I use the request: https://sourceforge.net/projects/kidbasic/best_release.json If you use this link into a browser you will get some json reply. So, this is the code:

app.pro

QT       += core gui
QT       += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = app
TEMPLATE = app
SOURCES += main.cpp\
        mainwindow.cpp
HEADERS  += mainwindow.h

main.cpp

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QPushButton>


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void replyFinished(QNetworkReply* reply);
    void checkForUpdate(void);
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget * wdg = new QWidget(this);
    QPushButton *train_button = new QPushButton(wdg);
    train_button->setText(tr("check update"));
    setCentralWidget(wdg);
    QObject::connect(train_button, SIGNAL(released()), this, SLOT(checkForUpdate()));
}

MainWindow::~MainWindow(){    }

void MainWindow::checkForUpdate(void){
    QNetworkRequest request;
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    config.setProtocol(QSsl::SecureProtocols);
    request.setSslConfiguration(config);

    request.setUrl(QUrl("https://sourceforge.net/projects/kidbasic/best_release.json"));
    //other urls to test
    //request.setUrl(QUrl("https://sourceforge.net/projects/openofficeorg.mirror/files/stats/json?start_date=2014-10-29&end_date=2014-11-04"));
    //request.setUrl(QUrl("https://ro.gravatar.com/205e460b479e2e5b48aec07710c08d50.json"));
    //request.setUrl(QUrl("http://headers.jsontest.com/"));

    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    manager->get(request);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}


void MainWindow::replyFinished(QNetworkReply* reply){
    qDebug() << reply->error();
    if(reply->error() == QNetworkReply::NoError) {
        //process json reply
        QString strReply = (QString)reply->readAll();
        qDebug() << strReply;
    } else {
        qDebug() << "ERROR";
    }
    reply->deleteLater();
}

(To make it work without warnings I take libeay32.dll and ssleay32.dll from https://indy.fulgan.com/SSL/ and put those files in the same folder with my application - because of https).

If I try with others urls I got the correct reply. If I use any surceforge.net request, I got this error: QNetworkReply::NetworkError(RemoteHostClosedError).

I make the same requests with https://www.hurl.it and everything seems to be ok. The same results I got with https://httpstatus.io/ too.

How I can get a valid reply from surceforge.net with QT?


Solution

  • sourceforge.netseems to be closing the connection when the HTTP request's user agent string is Mozilla/5.0 (this is the default user agent used by QNetworkAccessManager when no QNetworkRequest::UserAgentHeader has been set).

    You might have to set a custom user agent before sending the request:

    request.setHeader(QNetworkRequest::UserAgentHeader, "App/1.0");