Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Last active August 25, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/cf6c61eae879b7a5a2902b49d0ed1841 to your computer and use it in GitHub Desktop.
Save eyllanesc/cf6c61eae879b7a5a2902b49d0ed1841 to your computer and use it in GitHub Desktop.
#include "downloader.h"
Downloader::Downloader(const QUrl &url, QObject *parent) : QObject(parent)
{
auto manager = new QNetworkAccessManager(this);
QNetworkRequest request(url);
connect(manager, &QNetworkAccessManager::finished, this, &Downloader::replyFinished);
manager->get(request);
}
void Downloader::replyFinished(QNetworkReply *reply)
{
QUrl redirect= reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
if (!redirect.isEmpty() && redirect.isValid() && reply->url() != redirect){
qDebug()<<redirect;
if(redirect.isRelative())
redirect = reply->url().resolved(redirect);
reply->manager()->get(QNetworkRequest(redirect));
return;
}
qDebug()<<"finished";
const auto newVersion = QString(reply->readAll());
qDebug()<<newVersion;
}
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QNetworkReply>
#include <QObject>
class Downloader : public QObject
{
Q_OBJECT
public:
explicit Downloader(const QUrl &url, QObject *parent = nullptr);
public slots:
void replyFinished(QNetworkReply *reply);
};
#endif // DOWNLOADER_H
QT += core
QT -= gui
QT += network
CONFIG += c++11
TARGET = DropboxDownload
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
downloader.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
HEADERS += \
downloader.h
#include <QCoreApplication>
#include "downloader.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QUrl url("http://www.dropbox.com/s/ehoawgm89yz6pib/version.txt?raw=1");
Downloader downloader(url);
return a.exec();
}
@eyllanesc
Copy link
Author

Output:

QUrl("https://www.dropbox.com/s/ehoawgm89yz6pib/version.txt?‌​raw=1") 
QUrl("https://dl.dropboxusercontent.com/content_link/qdWtaff‌​w8D12BrSySNkxdVxNyEg‌​GhAUJcmvLQjOggXVxDuC‌​EfaPWoBdnri75kfnC/fi‌​le") 
 finished 
"2017.09.01\n"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment