Simple http post request demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Simple http post request demo相关的知识,希望对你有一定的参考价值。

 1 // httppostdemo.h
 2 #ifndef HTTPPOSTDEMO_H
 3 #define HTTPPOSTDEMO_H
 4 
 5 #include <QMainWindow>
 6 #include <QNetworkAccessManager>
 7 #include <QNetworkReply>
 8 
 9 namespace Ui {
10 class HttpPostDemo;
11 }
12 
13 class HttpPostDemo : public QMainWindow
14 {
15     Q_OBJECT
16 
17 public:
18     explicit HttpPostDemo(QWidget *parent = 0);
19     ~HttpPostDemo();
20 
21 private slots:
22     void on_pushButton_clicked();
23     void slotFinished(QNetworkReply *reply);
24 
25 private:
26     Ui::HttpPostDemo *ui;
27     QNetworkAccessManager *m_manager;
28 };
29 
30 #endif // HTTPPOSTDEMO_H
 1 // httppostdemo.cpp
 2 #include "httppostdemo.h"
 3 #include "ui_httppostdemo.h"
 4 
 5 #include <QDebug>
 6 #include <QNetworkRequest>
 7 
 8 HttpPostDemo::HttpPostDemo(QWidget *parent) :
 9     QMainWindow(parent),
10     ui(new Ui::HttpPostDemo)
11 {
12     ui->setupUi(this);
13 
14     m_manager = new QNetworkAccessManager(this);
15     connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinished(QNetworkReply*)));
16 }
17 
18 HttpPostDemo::~HttpPostDemo()
19 {
20     delete ui;
21 }
22 
23 void HttpPostDemo::slotFinished(QNetworkReply *reply)
24 {
25     qDebug() << reply->readAll();
26 }
27 
28 void HttpPostDemo::on_pushButton_clicked()
29 {
30     QNetworkRequest *req = new QNetworkRequest();
31     req->setUrl(QUrl("http://zc.7k7k.com/post_login"));
32     req->setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded; charset=UTF-8");
33     req->setRawHeader("Accept","application/json, text/javascript, */*; q=0.01");
34     req->setRawHeader("Accept-Language","zh-CN,zh;q=0.8");
35     req->setRawHeader("X-Requested-With","XMLHttpRequest");
36     req->setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
37     req->setRawHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
38     // req->setRawHeader("Accept-Encoding","gzip,deflate");
39     req->setRawHeader("Host","zc.7k7k.com");
40     req->setRawHeader("Connection","Keep-Alive");
41     req->setRawHeader("Cache-Control","no-cache");
42     QByteArray data;
43     data.append("username=username&password=password&autologin=checked&rf=http://www.7k7k.com/#bottom");
44     m_manager->post(*req, data);
45 }
 1 // main.cpp
 2 #include "httppostdemo.h"
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     HttpPostDemo w;
 9     w.show();
10 
11     return a.exec();
12 }

以上是关于Simple http post request demo的主要内容,如果未能解决你的问题,请参考以下文章

第八章

HTTP requests.post 超时

http post by requests

requests接口测试-requests的安装

http.request请求及在node中post请求参数解析

HTTP: Request中的post和get区别