QT中Http的阻塞型Get和Post
QT中实现http请求阻塞型(Http请求都在非GUI线程中)这个时候就可以用阻塞型,方便使用
一下代码都是验证过的,包含.h和源代码,时间超时等进行判断
主要是阻塞型的时候需要各种设置
do{
QApplication::processEvents();//该线程的事件循环
if(DiffTime(lasttime,GetTickCountSYS())>=m_TimeOut){
m_pRelay->deleteLater();
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
m_pNetworkAccessManager = NULL;
return false;//超时了
}
}while(m_HtmlState != s_HTMLSTATE_END);//!m_pRelay->isFinished(),该标记完成的时候,不代表代表已经执行了槽
include “httpbase.h”
include
include “Bootloader/SYSInterface.h”
define _T(x) QString::fromLocal8Bit(##x##)
CHttpBase::CHttpBase()
{
/*
httpRequest.setRawHeader(“Accept”, API_ACCEPT);
httpRequest.setRawHeader(“User-Agent”, API_USER_AGENT);
httpRequest.setRawHeader(“X-XXX-API-Key”, API_KEY);
httpRequest.setRawHeader(“X-XXX-API-Secret”, API_SECRET);
httpRequest.setRawHeader(“Accept-Encoding”, “gzip, deflate”);
httpRequest.setRawHeader(“Content-Type”, “application/json”);
settings = new QSettings("XXX");
QString id = settings->value(SETTING_ACCOUNT_ID, "").toString();
QString token = settings->value(SETTING_ACCOUNT_TOKEN, "").toString();
if(!id.isEmpty()) {
httpRequest.setRawHeader("X-XXX-User-ID", id.toStdString().c_str());
}
if (!token.isEmpty()) {
httpRequest.setRawHeader("X-XXX-User-Token", token.toStdString().c_str());
}
qDebug() << "BaseAPI...id:" << id << " token:" + token;
*/
m_HtmlErrExplain.insert(0,_T(“No ERROR”));
m_HtmlErrExplain.insert(4,_T(“连接远程服务器超时”));
m_HtmlErrExplain.insert(201,_T(“远程服务器拒绝连接”));
m_HtmlErrExplain.insert(203,_T(“远程服务器不能发现”));
m_pNetworkAccessManager = NULL;
m_TimeOut = 3000;
}
CHttpBase::~CHttpBase()
{
if(m_pNetworkAccessManager){
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
}
}
///进入时生命一个,退出时删除,保证不同线程可以使用
bool CHttpBase::get(const QString url)
{
if(m_pNetworkAccessManager == NULL) m_pNetworkAccessManager = new QNetworkAccessManager();
m_HtmlState = s_HTMLSTATE_START;
QNetworkRequest httprequest;
httprequest.setHeader(QNetworkRequest::ContentTypeHeader,”application/x-www-form-urlencoded”);
QObject::connect(GetNetworkAccessManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
httprequest.setUrl(url);
m_pRelay = GetNetworkAccessManager()->get(httprequest);
DWORD lasttime = GetTickCountSYS();
do{
QApplication::processEvents();//该线程的事件循环
if(DiffTime(lasttime,GetTickCountSYS())>=m_TimeOut){
m_pRelay->deleteLater();
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
m_pNetworkAccessManager = NULL;
return false;//超时了
}
}while(m_HtmlState != s_HTMLSTATE_END);//!m_pRelay->isFinished(),该标记完成的时候,不代表代表已经执行了槽
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
m_pNetworkAccessManager = NULL;
return true;
}
bool CHttpBase::get(const QString url, int trytimes)
{
do{
if(get(url)) return true;
}while((trytimes–)>0);
return false;
}
bool CHttpBase::post(const QString url, const QByteArray &data)
{
if(m_pNetworkAccessManager != NULL){
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
m_pNetworkAccessManager = NULL;
}
if(m_pNetworkAccessManager == NULL) m_pNetworkAccessManager = new QNetworkAccessManager();
m_HtmlState = s_HTMLSTATE_START;
QNetworkRequest httprequest;
httprequest.setHeader(QNetworkRequest::ContentTypeHeader,”application/x-www-form-urlencoded”);
QObject::connect(GetNetworkAccessManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
httprequest.setUrl(QUrl(url));
m_pRelay = GetNetworkAccessManager()->post(httprequest,data);
DWORD lasttime = GetTickCountSYS();
do{
QApplication::processEvents();//该线程的事件循环
if(DiffTime(lasttime,GetTickCountSYS())>=m_TimeOut){
m_pRelay->deleteLater();
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
m_pNetworkAccessManager = NULL;
return false;//超时了
}
}while(m_HtmlState != s_HTMLSTATE_END);//该标记完成的时候,是否代表已经执行了槽
m_pNetworkAccessManager->disconnect();
delete m_pNetworkAccessManager;
m_pNetworkAccessManager = NULL;
return true;
//httpRequest.setUrl(QUrl(url));
// m_pRelay = networkAccessManager.post(httpRequest, data);
}
bool CHttpBase::post(const QString url, const QByteArray &data, int trytimes)
{
do{
if(post(url,data)) return true;
}while((trytimes–)>0);
return false;
}
QString CHttpBase::GetNetworkStateExplain(int statecode)
{
QMap
ifndef CHTTPBASE_H
define CHTTPBASE_H
include
include
include
include
include
include
include
include
class CHttpBase : public QObject
{
Q_OBJECT
public:
//explicit CHttpBase(QObject *parent = 0);
explicit CHttpBase();
~CHttpBase();
int m_HtmlState;/连接状态/
static const int s_HTMLSTATE_END = 0;
static const int s_HTMLSTATE_START = 1;
unsigned long m_TimeOut;
bool get(const QString url);//false–>超时了
bool get(const QString url,int trytimes);//false–>超时了
bool post(const QString url, const QByteArray &data);//false->超时了
bool post(const QString url, const QByteArray &data,int trytimes);//false->超时了,trytimes–>0请求一次,1代表请求了2次
QString GetNetworkStateExplain(int statecode);
void TimeOutHandle();
QMap
endif // CHTTPBASE_H
“`
上一篇: 走迷宫程序
推荐阅读
-
如何解决Ajax访问不断变化的session的值不一致以及HTTP协议中的GET、POST的区别
-
vue 2.x 中axios 封装的get 和post方法
-
微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
-
HTTP: Request中的post和get区别
-
有关Ajax中get和post的使用问题
-
php中运用http调用的GET和POST方法示例
-
jquery中get和post的简单实例
-
jquery中get,post和ajax方法的使用小结
-
关于HTTP中Get与Post的区别讲解
-
HTTP中get和post