Skip to content

Instantly share code, notes, and snippets.

@Vi-dot
Created February 26, 2019 13:45
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 Vi-dot/3fe948769377499b1253241848e19078 to your computer and use it in GitHub Desktop.
Save Vi-dot/3fe948769377499b1253241848e19078 to your computer and use it in GitHub Desktop.
Http async request with QThread
#include "http.h"
#include "httpexec.h"
Http::Http(QObject *parent) : QObject(parent)
{
m_thread = NULL;
m_reply = NULL;
}
Http::~Http() {
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
}
if (m_thread)
m_thread->requestInterruption();
}
void Http::exec()
{
if (m_reply)
m_reply->abort();
if (m_thread)
m_thread->requestInterruption();
m_thread = new QThread();
HttpExec* exec = new HttpExec(this);
connect(m_thread, SIGNAL(finished()), exec, SLOT(deleteLater()));
connect(m_thread, SIGNAL(finished()), m_thread, SLOT(deleteLater()));
connect(m_thread, &QThread::started, exec, &HttpExec::run);
exec->moveToThread(m_thread);
m_thread->start();
}
#ifndef HTTP_H
#define HTTP_H
#include <QObject>
#include <QNetworkReply>
#include <QThread>
class Http : public QObject
{
Q_OBJECT
public:
Http(QObject *parent = 0);
~Http();
Q_INVOKABLE void exec();
QNetworkReply* m_reply;
protected:
QThread* m_thread;
public slots:
void onFinished();
void onError(QNetworkReply::NetworkError code);
void onSslErrors(const QList<QSslError> &errors);
};
#endif // HTTP_H
#include "httpexec.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QEventLoop>
HttpExec::HttpExec(Http* http) : QObject()
{
m_http = http;
}
void HttpExec::run() {
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
QNetworkRequest request;
if (m_http->m_type == Http::Type::GET) {
/** set url, params.. **/
m_reply = manager->get(request);
}
else if (m_http->m_type == Http::Type::POST || m_http->m_type == Http::Type::PUT) {
/** set url, params.. **/
if (m_http->m_type == Http::Type::PUT)
m_reply = manager->put(request, params.toJson());
else
m_reply = manager->post(request, params.toJson());
}
if (m_http->m_reply)
m_http->m_reply->deleteLater();
m_reply->setParent(NULL);
m_http->m_reply = m_reply;
QObject::connect(m_reply, SIGNAL(finished()), m_http, SLOT(onFinished()));
QObject::connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), m_http, SLOT(onError(QNetworkReply::NetworkError)));
QObject::connect(m_reply, SIGNAL(sslErrors(const QList<QSslError> &)), m_http, SLOT(onSslErrors(const QList<QSslError> &)));
QEventLoop loop;
QObject::connect(m_reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
manager->deleteLater();
}
#ifndef HTTPEXEC_H
#define HTTPEXEC_H
#include "http.h"
#include <QRunnable>
#include <QNetworkReply>
class HttpExec : public QObject
{
public:
HttpExec(Http* http);
void run();
private:
Http* m_http;
QNetworkReply* m_reply;
};
#endif // HTTPEXEC_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment