1. 程式人生 > >Qt4 如何使用QHttp實現post和get

Qt4 如何使用QHttp實現post和get

nt標頭檔案:

// http.h
#ifndef HTTP_H
#define HTTP_H
#include <QObject>
#include <QBuffer>
#include <QHttp>
#include <QUrl>

class Http: public QObject
{
    Q_OBJECT

public:
    Http(QObject *parent = 0);
    ~Http();

    void setUrl(const QUrl &url);
    void setPort(quint16 port)
; void get(); void post(const QString &script); bool error(); QByteArray read(); protected: QHttp *http; QUrl _url; QBuffer reply; quint16 _port; int _id; bool _error; protected slots: void requestStarted(int); void requestFinished(int, bool); void
dataSendProgess(int, int); void dataReadProgess(int, int); void responseHeaderReceived(); signals: void done(); } #endif // HTTP_H

原始檔:

// http.cpp
#include "http.h"
#include <iostream>

Http::Http(QObject *parent)
    :QObject(parent)
{
    http = new QHttp(this);
    _port = 80;

    connect
(http,SIGNAL(requestStarted(int)), this,SLOT(requestStarted(int))); connect(http,SIGNAL(requestFinished(int,bool)), this,SLOT(requestFinished(int,bool))); connect(http,SIGNAL(dataSendProgress(int,int)), this,SLOT(dataSendProgess(int,int))); connect(http,SIGNAL(dataReadProgress(int,int)), this,SLOT(dataReadProgess(int,int))); } Http::~Http() { delete http; disconnect(this); QObject::~QObject(); } void Http::setUrl(const QUrl &url) {_url = url;} void Http::setPort(quint16 port) {_port = port;} void Http::get() { http->setHost(_url.host(),_port); _id = http->get(_url.path(),&reply); } void Http::post(const QString &script) { http->setHost(_url.host(),_port); _id = http->post(_url.path(),script,&reply); } bool Http::error() {return _error;} QByteArray Http::read() {return reply;} void Http::requestStarted(int id) { if (id != _id) return; std::cout << "正在連線至伺服器" << qPrintable(_url.host()) << " ..." << std::endl; } void Http::requestFinished(int id, bool error) { if (id != _id) return; std::cout << "正在斷開與伺服器" << qPrintable(_url.host()) << "的連線 ..." << std::endl; if (!error) std::cout << "成功與" << qPrintable(_url.host()) << "交換資料" << std::endl; else { switch (http->error()) { case QHttp::HostNotFound: std::cerr << "找不到指定伺服器" << qPrintable(_url.host()) << std::endl;break; case QHttp::ConnectionRefused: std::cerr << "伺服器" << qPrintable(_url.host()) << "拒絕連線請求" << std::endl;break; case QHttp::UnexpectedClose: std::cerr << "與伺服器" << qPrintable(_url.host()) << "的連線被伺服器意外地關閉" << std::endl;break; case QHttp::InvalidResponseHeader: std::cerr << "響應檔案" << qPrintable(_url.path()) << "無效" << std::endl;break; case QHttp::WrongContentLength: std::cerr << "資料長度失效" << std::endl;break; case QHttp::Aborted: std::cerr << "與伺服器" << qPrintable(_url.host()) << "的連線突然中斷" << std::endl;break; case QHttp::ProxyAuthenticationRequiredError: std::cerr << "與代理伺服器的連線需要身份驗證" << std::endl; case QHttp::AuthenticationRequiredError: std::cerr << "與伺服器" << qPrintable(_url.host()) << "的連線需要身份驗證" << std::endl;break; default: std::cerr << "發生了一個未知錯誤" << std::endl;break; } } } void Http::dataSendProgess(int done, int total) { std::cout << "正在向伺服器" << qPrintable(_url.host()) << "傳送資料 ..." << std::endl << "總資料大小: " << total << "KB" << std::endl << "已傳送資料大小: " << done << "KB" << std::endl << "待發送資料大小: " << total-done << "KB" << std::endl << "已傳送資料佔比: " << 100*done/total << "%" << std::endl << "待發送資料佔比: " << 100-100*done/total << "%" << std::endl; } void Http::dataReadProgess(int done, int total) { std::cout << "正在從伺服器" << qPrintable(_url.host()) << "接受資料 ..." << std::endl; if (total) std::cout << "總資料大小: " << total << std::endl; std::cout << "已接受資料大小: " << done << std::endl; if (total) std::cout << "待接收資料大小: " << total-done << std::endl << "已接受資料佔比: " << 100*done/total << "%" << std::endl << "待接受資料佔比: " << 100-100*done/total << "%" << std::endl; }

程式碼自己理解吧,這裡發一個QHttp幫助文件的連結,可以自己去看一看,不過是全英文的,可以用瀏覽器翻譯。這裡給出一張post過程的流程圖。

Created with Raphaël 2.2.0開始執行post發射requestStarted(id)訊號是否找到指定伺服器指定檔案是否有效上傳資料發射dataSendProgress(done,total)訊號資料是否上傳完畢發射responseHeaderReceived(responseHeader)訊號發射dataReadProgress(done,total)訊號資料是否接受完畢發射readyRead()訊號發射requestFinished(id,true)訊號發射done(true)訊號結束發射requestFinished(id,false)訊號發射done(false)訊號yesnoyesnoyesnoyesno