主要是阻塞型的时候需要各种设置
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(),该标记完成的时候,不代表代表已经执行了槽
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
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
“`