QT make app检查sourceforge.net是否有更新

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

我希望我的 QT 应用程序检查 sourceforge.net 上是否有更新。为此,我使用请求:https://sourceforge.net/projects/kidbasic/best_release.json 如果你在浏览器中使用这个 link 你会得到一些 json 回复。 所以,这是代码:

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();
}

(为了让它在没有警告的情况下工作,我从 https://indy.fulgan.com/SSL/ 中取出 libeay32.dllssleay32.dll 并将这些文件与我的应用程序放在同一个文件夹中 - 因为 https ).

如果我尝试使用其他网址,我会得到正确的答复。如果我使用任何 surceforge.net 请求,我会收到此错误:QNetworkReply::NetworkError(RemoteHostClosedError).

我也对 https://www.hurl.it and everything seems to be ok. The same results I got with https://httpstatus.io/ 提出了同样的要求。

如何使用 QT 从 surceforge.net 获得有效回复?

sourceforge.net似乎在 HTTP 请求的用户代理字符串为 Mozilla/5.0 时关闭连接(这是 QNetworkAccessManager 在没有 QNetworkRequest::UserAgentHeader 时使用的默认用户代理已设置)。

您可能必须在发送请求之前设置自定义用户代理:

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