qt中发送http请求
分类:C++, Others, Qt
阅读 (5,155)
Add comments
8月 172018
在QT中发送http请求要使用下面的几个类
- QNetworkAccessManager – 用于实际发送网络请求
- QNetworkRequest – 用于构造http请求的内容
- QNetworkReply – 用于构造http响应的内容
首先要在项目文件里增加network的支持,在xxx.pro里面加上如下代码
1 2 |
QT += core gui QT += network |
下面是具体的代码:
logindialog.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#ifndef LOGINDIALOG_H #define LOGINDIALOG_H #include <QDialog> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QNetworkRequest> #include <QUrl> #include "constants.h" #include "jsonobjs/jsonloginres.h" namespace Ui { class LoginDialog; } class LoginDialog : public QDialog { Q_OBJECT public: explicit LoginDialog(QWidget *parent = 0); ~LoginDialog(); private slots: void on_btnLogin_clicked(); void onLoginReqFinished(); private: Ui::LoginDialog *ui; QNetworkAccessManager* mNam = NULL; QNetworkReply* mLoginReply = NULL; void reqLogin(); }; #endif // LOGINDIALOG_H |
logindialog.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#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(); } |