[Qt] Qt实现HTTP请求 [2013-12-18更新]

- 通过QTcpSocket实现


实现原理:通过tcp socket向Web服务器的80端口发送HTTP请求字符串

/*********************************************************
    mainwindow.h
 *********************************************************/
#include <QMainWindow>
#include <QTcpSocket>
#include <QDebug>
 
  
namespace Ui {
class MainWindow;
}
 
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    QTcpSocket *tcpSocket;

private slots:
    void slotConnected();
    void slotReadyRead();
    void slotDisconnected();
};
 
  
 
  
/*********************************************************
    mainwindow.cpp
 *********************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
  
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    tcpSocket = new QTcpSocket(this);
    connect(tcpSocket, SIGNAL(connected()), this, SLOT(slotConnected()));
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
    connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
 
  
    
    // 假设Web服务器的IP地址为192.168.1.239
    tcpSocket->connectToHost("192.168.1.239", 80);
}
 
  
MainWindow::~MainWindow()
{
    delete ui;
}
 
  
void MainWindow::slotConnected()
{
    qDebug() << "connected";
    tcpSocket->write("GET / HTTP/1.1\r\n"   // “/”表示访问默认主页,可修改为“/cgi-bin/test.cgi”等等
                     "Connection: Keep-Alive\r\n"
                     "Accept-Encoding: gzip\r\n"
                     "Accept-Language: zh-CN,en,*\r\n"
                     "User-Agent: Mozilla/5.0\r\n"
                     "Host: 192.168.1.239\r\n\r\n");  // Host必须与目标Web服务器一致
}
 
  
void MainWindow::slotReadyRead()
{
    qDebug() << "readyread";
    qDebug() << QString(tcpSocket->readAll());
}
 
  
void MainWindow::slotDisconnected()
{
    qDebug() << "disconnected";
}
 
  
 
 


- 通过QNetworkAccessManager实现


/*********************************************************
    mainwindow.h
 *********************************************************/
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>
 
   
namespace Ui {
class MainWindow;
}
 
   
class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *manager;
    QNetworkRequest *request;
    QNetworkReply *reply;
 
   
private slots:
    void slot_ReadyRead();
    void slot_ReplyFinished(QNetworkReply*);
};
 
   
 
   
/*********************************************************
    mainwindow.cpp
 *********************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
   
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    manager = new QNetworkAccessManager(this);
    request = new QNetworkRequest(QUrl("http://192.168.1.239/cgi-bin/out.cgi?name=name&ok=ok"));
    reply = manager->get(*request);
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slot_ReplyFinished(QNetworkReply*)));
    connect(reply, SIGNAL(readyRead()), this, SLOT(slot_ReadyRead()));
}
 
   
MainWindow::~MainWindow()
{
    delete ui;
}
 
   
void MainWindow::slot_ReplyFinished(QNetworkReply *reply)
{
    qDebug() << "slot_ReplyFinished";
    qDebug() << QString(reply->readAll()).simplified();
}
 
   
void MainWindow::slot_ReadyRead()
{
    qDebug() << "slot_ReadyRead";
}


- 两种实现方式的比较


1. 通过QTcpSocket实现有助于理解Web服务器的工作方式和HTTP协议,但是返回的结果需要自己去掉HTTP协议头。
2. 通过QNetworkAccessManager实现屏蔽了内部细节,返回的结果会自动去掉HTTP协议头。


你可能感兴趣的:([Qt] Qt实现HTTP请求 [2013-12-18更新])