Netcontroller class

This commit is contained in:
才明洋
2018-03-16 20:39:43 +08:00
parent 13233c8e10
commit db00144d82
6 changed files with 219 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
#-------------------------------------------------
#
# Project created by QtCreator 2018-03-16T19:31:11
#
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NEU
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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
SOURCES += \
main.cpp \
mainwindow.cpp \
netcontroller.cpp
HEADERS += \
mainwindow.h \
netcontroller.h
+13
View File
@@ -0,0 +1,13 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
NetController controller("", "");
controller.sendLogoutRequest();
return a.exec();
}
+11
View File
@@ -0,0 +1,11 @@
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <netcontroller.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
+103
View File
@@ -0,0 +1,103 @@
#include "netcontroller.h"
NetController::NetController(QByteArray id, QByteArray passwd, QObject *parent) : QObject(parent),
username(id), password(passwd), flag(Disconnected), manager(this), checkTimer(new QTimer(this))
{
checkParam.append("action=get_online_info");
loginParam.append("action=login&");
loginParam.append("nas_ip=&");
loginParam.append("password=");
loginParam.append(password);
loginParam.append("&save_me=0&");
loginParam.append("url=&");
loginParam.append("user_ip=&");
loginParam.append("user_mac=&");
loginParam.append("username=");
loginParam.append(username);
logoutParam.append("action=logout&");
logoutParam.append("ajax=1&");
logoutParam.append("password=");
logoutParam.append(password);
logoutParam.append("&username=");
logoutParam.append(username);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
connect(&manager, QNetworkAccessManager::finished, this, handleResponse);
checkTimer->setInterval(1000);
connect(checkTimer, QTimer::timeout, this, checkStatus);
checkTimer->start();
}
void NetController::checkStatus()
{
sendCheckRequest();
if(NEUStatus==Offline&&flag!=Offline)
{
flag = Offline;
emit getOffline();
qDebug()<<"turn Offline";
}
else if(NEUStatus == Online && flag != Online)
{
flag = Online;
emit getOnline();
qDebug()<<"turn Online";
}
else if(NEUStatus == Disconnected && flag != Disconnected)
{
flag = Disconnected;
emit getDisconnected();
qDebug()<<"turn Disconnected";
}
}
void NetController::sendCheckRequest()
{
request.setUrl(QUrl(checkUrl));
manager.post(request, checkParam);
}
void NetController::sendLoginRequest()
{
request.setUrl(QUrl(loginUrl));
manager.post(request, loginParam);
}
void NetController::sendLogoutRequest()
{
request.setUrl(QUrl(loginUrl));
manager.post(request, logoutParam);
}
void NetController::handleResponse(QNetworkReply *reply)
{
// qDebug()<<reply->url();
QString url = reply->url().toString();
reply->deleteLater();
if(url==checkUrl)
{
if(reply->error()==QNetworkReply::NoError)
{
QString status = reply->readAll();
if(status==offlineString)
{
NEUStatus = Offline;
}
else
{
NEUStatus = Online;
}
}
else
{
NEUStatus = Disconnected;
}
}
else if(url==loginUrl)
{
}
}
+42
View File
@@ -0,0 +1,42 @@
#ifndef NETCONTROLLER_H
#define NETCONTROLLER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>
class NetController : public QObject
{
Q_OBJECT
public:
explicit NetController(QByteArray username, QByteArray password, QObject *parent = nullptr);
void checkStatus();
void sendCheckRequest();
void sendLoginRequest();
void sendLogoutRequest();
enum Status {Offline, Online, Disconnected};
private:
QString checkUrl = "http://ipgw.neu.edu.cn/include/auth_action.php";
QString loginUrl = "http://ipgw.neu.edu.cn/srun_portal_pc.php?ac_id=1&";
QString offlineString = "not_online";
QByteArray checkParam, loginParam, logoutParam, username, password;
Status NEUStatus, flag;
QNetworkAccessManager manager;
QNetworkRequest request;
QTimer *checkTimer;
signals:
void getOnline();
void getOffline();
void getDisconnected();
public slots:
void handleResponse(QNetworkReply *reply);
};
#endif // NETCONTROLLER_H