#include "logindialog.h"
#include "ui_logindialog.h"
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
mNam = new QNetworkAccessManager(this);
//连接返回的信号槽
//connect(mNam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onLoginReqFinished(QNetworkReply*)));
}
LoginDialog::~LoginDialog()
{
delete mLoginReply;
delete mNam;
delete ui;
}
void LoginDialog::on_btnLogin_clicked()
{
reqLogin();;
}
void LoginDialog::reqLogin()
{
if(NULL != mLoginReply && !mLoginReply->isFinished()){
return;
}
QNetworkRequest request;
std::string url("http://bcoder.com/loginapi");
QByteArray params ;
params.append("ucode=" + ui->edtUserName->text());
params.append("&password=" + ui->edtPassword->text());
request.setUrl(QUrl(url.c_str()));
//request.setRawHeader("Content-Type","application/json");
request.setRawHeader("Content-Type","application/x-www-form-urlencoded");
// request->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
// request->setRawHeader("Referer","http://localhost:8888/login");
// request->setRawHeader("Accept-Language","zh-CN");
// request->setRawHeader("X-Requested-With","XMLHttpRequest");
// request->setRawHeader("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
// request->setRawHeader("Content-Type","application/x-www-form-urlencoded");
// request->setRawHeader("Accept-Encoding","gzip,deflate");
// request->setRawHeader("Host","localhost:8888");
// request->setRawHeader("Content-Length","18");
// request->setRawHeader("Connection","Keep-Alive");
// request->setRawHeader("Cache-Control","no-cache");
mLoginReply = mNam->post(request, params);
connect(mLoginReply, SIGNAL(finished()), this, SLOT(onLoginReqFinished()));
}
void LoginDialog::onLoginReqFinished()
{
QByteArray bytes = mLoginReply->readAll(); // bytes
//QString string(bytes); // string
QString string = QString::fromUtf8(bytes);
JsonLoginRes res(string.toStdString());
qDebug() << "---------------------------------\n" << string;
mLoginReply->close();
// mLoginReply->deleteLater();
}