#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "datadboperation.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString strUserInfo = QString("name=%1&password=%2").arg(user).arg(passward);
QByteArray content = strUserInfo.toUtf8();
int contentLength = content.length();
QNetworkRequest netReq;
netReq.setUrl(QUrl("server ip address"));
netReq.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
netReq.setHeader(QNetworkRequest::ContentLengthHeader, contentLength);
// 将用户名和密码发送至web服务器进行验证
networkAccessManager = new QNetworkAccessManager(this);
// 发送参数
networkAccessManager->post(netReq, content);
connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(SltLoginReplay(QNetworkReply*)));
}
void MainWindow::SltLoginReplay(QNetworkReply *reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << "statusCode:" << statusCode;
if(reply->error() == QNetworkReply::NoError)
{
qDebug() << reply->readAll();
}
else
{
qDebug() << "=========";
}
// At the end of that slot, we won‘t need it anymore
reply->deleteLater();
}
MainWindow::~MainWindow()
{
delete ui;
}
原文地址:http://blog.51cto.com/whylinux/2113027