我是靠谱客的博主 舒服哑铃,最近开发中收集的这篇文章主要介绍基于qt的带cookie同步、异步post及get的http请求,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

//单例
pragma  once
#include <QMutex>
#include <QScopedPointer>

template <typename T>
class YxSingleton
{
public:
	static T &getInstance();
	YxSingleton(const YxSingleton &other) = default;
	YxSingleton<T> &operator=(const YxSingleton &other) = default;
private:
	static QMutex mutex;
	static QScopedPointer<T> instance;
};

template <typename T> QMutex YxSingleton<T>::mutex;
template <typename T> QScopedPointer<T> YxSingleton<T>::instance;

template<typename T>
T &YxSingleton<T>::getInstance()
{
	if (instance.isNull()) {
		mutex.lock();
		if (instance.isNull()) {
			instance.reset(new T());
		}
		mutex.unlock();
	}
	return *instance.data();
}

#define SINGLETON(Class) 
    private: 
    Class(); 
    ~Class(); 
    Class(const Class &other); 
    Class &operator =(const Class &other); 
    friend class YxSingleton<Class>; 
    friend struct QScopedPointerDeleter<Class>;
#pragma once
#include <QObject>
#include <map>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include <QTimer>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include "YxSingleton.h"

class YxNetworkCookieJar : public QNetworkCookieJar
{
public:
	YxNetworkCookieJar();
	QList<QNetworkCookie> getCookies();
	void setCookies(const QList<QNetworkCookie>& cookieList);
};

class YxReplyTimeout : public QObject
{
	Q_OBJECT
public:
	YxReplyTimeout(QNetworkReply *reply, const int timeout, bool _bansyc) : QObject(reply) {
		Q_ASSERT(reply);
		m_bansyc = _bansyc;
		if (reply && reply->isRunning()) {  // 启动单次定时器
			QTimer::singleShot(timeout, this, SLOT(onTimeout()));
		}
	}
signals:
	void timeout();  // 超时信号 - 供进一步处理
private slots:
	void onTimeout() {  // 处理超时
		QNetworkReply *reply = static_cast<QNetworkReply*>(parent());
		if (reply->isRunning()) {
			reply->abort();
			if (m_bansyc)
			{
				reply->deleteLater();
			}
			emit timeout();
		}
	}
private:
	bool m_bansyc;
};


class YxHttpAsyncRequest :public QObject
{
	SINGLETON(YxHttpAsyncRequest)
	Q_OBJECT
public:
	void RequestGet(QObject*_pObject, const QString&strUrl, std::function<void()> _FinishCallBack, std::function<void()> _timeoutCallBack, int _timeout);
	void RequestLoginPost(QObject*_pObject, std::function<void()> _FinishCallBack, std::function<void()> _timeoutCallBack, int _timeout);
	void RequestPostByCookie(QObject*_pObject, const QString&strUrl, const QString&strParam, std::function<void()> _FinishCallBack, std::function<void()> _timeoutCallBack, int _timeout);
	void SetLoginInfo(QString _strLoginUrl, QString _strLoginParam);
	void SetPlatfromAddr(const QString& _str);
	QString &GetPlatfromAddr();
	void RequestSyncGet(const QString&strUrl, QByteArray&retBuf, std::function<void()> _timeoutCallBack, int _timeout);
private:
	YxNetworkCookieJar m_WorkCookie;
	QString m_strLoginUrl;
	QString m_strLoginParam;
	QNetworkAccessManager m_NetManager;
	QString m_strPlatfromAddr;
};

#include "YxHttpAsyncRequest.h"
#include <QTextCodec>
#include <QEventLoop>

YxNetworkCookieJar::YxNetworkCookieJar()
{

}

QList<QNetworkCookie> YxNetworkCookieJar::getCookies()
{
	return allCookies();
}

void YxNetworkCookieJar::setCookies(const QList<QNetworkCookie>& cookieList)
{
	if (this == NULL) {
		return;
	}
	setAllCookies(cookieList);
}

YxHttpAsyncRequest::YxHttpAsyncRequest(){
	
}
YxHttpAsyncRequest::~YxHttpAsyncRequest(){}
void YxHttpAsyncRequest::RequestGet(QObject*_pObject, const QString&strUrl, std::function<void()> _FinishCallBack, std::function<void()> _timeoutCallBack, int _timeout)
{
	QNetworkRequest request;
	request.setUrl(QUrl(strUrl));
	QNetworkReply *reply = m_NetManager.get(request);

	connect(reply, &QNetworkReply::finished, _pObject, [=]() {
		_FinishCallBack();
	});
	YxReplyTimeout* sptrTimeout = new YxReplyTimeout(reply, _timeout, true);
	// 超时进一步处理
	connect(sptrTimeout, &YxReplyTimeout::timeout, [=]() {
		_timeoutCallBack();
	});
}

void YxHttpAsyncRequest::RequestLoginPost(QObject*_pObject, std::function<void()> _FinishCallBack, std::function<void()> _timeoutCallBack, int _timeout)
{
	QNetworkRequest request;
	request.setUrl(QUrl(m_strLoginUrl));
	request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
	QByteArray encodedString = QTextCodec::codecForName("UTF-8")->fromUnicode(m_strLoginParam);
	QNetworkReply *reply = m_NetManager.post(request, encodedString);
	connect(reply, &QNetworkReply::finished, _pObject, [=]() {
		_FinishCallBack();
	});
	YxReplyTimeout* sptrTimeout = new YxReplyTimeout(reply, _timeout, true);
	// 超时进一步处理
	connect(sptrTimeout, &YxReplyTimeout::timeout, [=]() {
		_timeoutCallBack();
	});
}

void YxHttpAsyncRequest::RequestPostByCookie(QObject*_pObject, const QString&strUrl, const QString&strParam, std::function<void()> _FinishCallBack, std::function<void()> _timeoutCallBack, int _timeout)
{
	QNetworkRequest request;
	request.setUrl(QUrl(strUrl));
	request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
	QByteArray encodedString = QTextCodec::codecForName("UTF-8")->fromUnicode(strParam);
	QNetworkReply *reply = m_NetManager.post(request, encodedString);
	connect(reply, &QNetworkReply::finished, _pObject, [=]() {
		_FinishCallBack();
	});
	YxReplyTimeout* sptrTimeout = new YxReplyTimeout(reply, _timeout, true);
	// 超时进一步处理
	connect(sptrTimeout, &YxReplyTimeout::timeout, [=]() {
		_timeoutCallBack();
	});
}

void YxHttpAsyncRequest::SetLoginInfo(QString _strLoginUrl, QString _strLoginParam)
{
	m_strLoginUrl = _strLoginUrl;
	m_strLoginParam = _strLoginParam;
}

void YxHttpAsyncRequest::SetPlatfromAddr(const QString& _str)
{
	m_strPlatfromAddr = _str;
}

QString & YxHttpAsyncRequest::GetPlatfromAddr()
{
	return m_strPlatfromAddr;
}

void YxHttpAsyncRequest::RequestSyncGet(const QString&strUrl, QByteArray&retBuf, std::function<void()> _timeoutCallBack, int _timeout)
{
	QNetworkRequest request;
	QNetworkAccessManager* pNetManager = new QNetworkAccessManager(this);
	request.setUrl(QUrl(strUrl));
	QNetworkReply *reply = pNetManager->get(request);

	QEventLoop eventLoop;
	connect(pNetManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
	YxReplyTimeout *pTimeout = new YxReplyTimeout(reply, _timeout, false);
	// 超时进一步处理
	connect(pTimeout, &YxReplyTimeout::timeout, [=]() {
		_timeoutCallBack();
	});
	eventLoop.exec();
	retBuf = reply->readAll();
	delete pTimeout;
	pTimeout = nullptr;
	delete reply;
	reply = nullptr;
	delete pNetManager;
	pNetManager = nullptr;
}

 

最后

以上就是舒服哑铃为你收集整理的基于qt的带cookie同步、异步post及get的http请求的全部内容,希望文章能够帮你解决基于qt的带cookie同步、异步post及get的http请求所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部