Qt post http請求使用
阿新 • • 發佈:2018-05-05
qt post
#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;
}
Qt post http請求使用