Qt网络编程之实例一GET方式

看了两天的Qt网络编程,其实主要就是看了看QNetworkAccessManagerQNetworkRequestQNetworkReply这三个类的主要内容。在之前,Qt网络编程主要是使用QHttp等类,但是现在在帮助手册中这些类已经标记为过时的,所以,现在用Qt编写网络程序最好还是使用上面的三个类,之前也说过,对于一个应用程序来说,一个QNetworkAccessManager已经足够了。不知道你有没有通过名字看出这三个类的联系呢?这里再贴一下三个类的官方说明,加强印象,也有助于大家对照接下来的示例看。

 

QNetworkAccessManager

QNetworkRequest

QNetworkReply

Allows the application to send network requests and receive replie

Holds a request to be sent with QNetworkAccessManager

Contains the data and headers for a request sent with QNetworkAccessManager

个人感觉三者关系可简单理解如下:

 

           接下来就看一个示例,其实主要内容还是Nokia给的示例,但是源程序有错误之处,我这里进行修改了,这里先贴一下效果:

         再来贴一下主要的代码,代码简单,一看就懂,呵呵:

 

[cpp]  view plain  copy
 
  1. #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.     QNetworkReply* reply = nam->get(QNetworkRequest(url));  
  27.     // NOTE: Store QNetworkReply pointer (maybe into caller).  
  28.     // When this HTTP request is finished you will receive this same  
  29.     // QNetworkReply as response parameter.  
  30.     // By the QNetworkReply pointer you can identify request and response.  
  31.   
  32. }  
  33.   
  34. void MainWindow::finishedSlot(QNetworkReply *reply)  
  35. {  
  36. #if 1  
  37.      // Reading attributes of the reply  
  38.      // e.g. the HTTP status code  
  39.      QVariant statusCodeV =  
  40.      reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);  
  41.      // Or the target URL if it was a redirect:  
  42.      QVariant redirectionTargetUrl =  
  43.      reply->attribute(QNetworkRequest::RedirectionTargetAttribute);  
  44.      // see CS001432 on how to handle this  
  45.   
  46.      // no error received?  
  47.      if (reply->error() == QNetworkReply::NoError)  
  48.      {  
  49.          // read data from QNetworkReply here  
  50.   
  51.          // Example 1: Creating QImage from the reply  
  52.          //QImageReader imageReader(reply);  
  53.          //QImage pic = imageReader.read();  
  54.   
  55.          // Example 2: Reading bytes form the reply  
  56.          QByteArray bytes = reply->readAll();  // bytes  
  57.          //QString string(bytes); // string  
  58.          QString string = QString::fromUtf8(bytes);  
  59.   
  60.          ui->textBrowser->setText(string);  
  61.      }  
  62.      // Some http error received  
  63.      else  
  64.      {  
  65.          // handle errors here  
  66.      }  
  67.   
  68.      // We receive ownership of the reply object  
  69.      // and therefore need to handle deletion.  
  70.      reply->deleteLater();  
  71. #endif  
  72. }  


      好了,今天就到了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值