Qt网络编程之实例二POST方式

  上一篇文章主要介绍了Qt网络编程的GET方式,这里再说一下POST方式。首先说一下自己的环境:

        主机:Win7;Web服务器:TomCat v7.x;数据库服务器:MySQL v5.x。

        主要是用Qt实现登录验证,后台使用servlet响应登录请求,查询后台数据库,是否为合法用户。相信大家明白了吧!我实现的就是客户端发出请求,服务器端在数据库中进行查询,如果查找到,则返回用户信息,如果没有找到,则返回0,首先贴一下效果图:

        1、在浏览器中测试:

         2、在Qt客户端中测试:

        这里贴一下Qt客户端的代码,服务器端的代码可详见另一篇博文:Android Tomcat 的应用之服务器部分

 

[cpp]  view plain  copy
 
  1. <span style="font-size:18px;">#include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3.   
  4. #include <QNetworkReply>  
  5. #include <QNetworkRequest>  
  6.   
  7. MainWindow::MainWindow(QWidget *parent) :  
  8.     QMainWindow(parent),  
  9.     ui(new Ui::MainWindow)  
  10. {  
  11.     ui->setupUi(this);  
  12.   
  13.     nam = new QNetworkAccessManager(this);  
  14.     QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),  
  15.              this, SLOT(finishedSlot(QNetworkReply*)));  
  16. }  
  17.   
  18. MainWindow::~MainWindow()  
  19. {  
  20.     delete ui;  
  21. }  
  22.   
  23. void MainWindow::on_pushButton_clicked()  
  24. {  
  25.     //QUrl url("http://www.hust.edu.cn/");  
  26.     //QUrl url("http://www.google.com/ig/api?weather=wuhan");  
  27.     QUrl url("http://localhost:8080/WebRoot/servlet/LoginServlet");  
  28.     QByteArray append("username=admin&password=123456");  
  29.     //QNetworkReply* reply = nam->get(QNetworkRequest(url));  
  30.     QNetworkReply* reply = nam->post(QNetworkRequest(url), append);  
  31.     // NOTE: Store QNetworkReply pointer (maybe into caller).  
  32.     // When this HTTP request is finished you will receive this same  
  33.     // QNetworkReply as response parameter.  
  34.     // By the QNetworkReply pointer you can identify request and response.  
  35.   
  36. }  
  37.   
  38. void MainWindow::finishedSlot(QNetworkReply *reply)  
  39. {  
  40. #if 1  
  41.      // Reading attributes of the reply  
  42.      // e.g. the HTTP status code  
  43.      QVariant statusCodeV =  
  44.      reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);  
  45.      // Or the target URL if it was a redirect:  
  46.      QVariant redirectionTargetUrl =  
  47.      reply->attribute(QNetworkRequest::RedirectionTargetAttribute);  
  48.      // see CS001432 on how to handle this  
  49.   
  50.      // no error received?  
  51.      if (reply->error() == QNetworkReply::NoError)  
  52.      {  
  53.          // read data from QNetworkReply here  
  54.   
  55.          // Example 1: Creating QImage from the reply  
  56.          //QImageReader imageReader(reply);  
  57.          //QImage pic = imageReader.read();  
  58.   
  59.          // Example 2: Reading bytes form the reply  
  60.          QByteArray bytes = reply->readAll();  // bytes  
  61.          //QString string(bytes); // string  
  62.          QString string = QString::fromUtf8(bytes);  
  63.   
  64.          ui->textBrowser->setText(string);  
  65.      }  
  66.      // Some http error received  
  67.      else  
  68.      {  
  69.          // handle errors here  
  70.      }  
  71.   
  72.      // We receive ownership of the reply object  
  73.      // and therefore need to handle deletion.  
  74.      reply->deleteLater();  
  75. #endif  
  76. }  
  77. </span>  


              好了,今天就到这吧

转载于:https://www.cnblogs.com/lifan3a/articles/7692295.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值