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)信号 yes no yes no yes no yes no
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值