基于Qt的网络编程(Http)

Qt QNetworkAccessManager封装了网络相关的api,本文主要简单介绍http请求。发起请求,响应返回,超时机制。

涉及类:QNetworkAccessManager、QNetworkReply、QNetworkRequest、QTimer。

程序截图如下:

基于Qt的网络编程(Http)_第1张图片


Http相关代码如下:

HttpFun.h

#ifndef HTTPFUN_H
#define HTTPFUN_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//http请求类
class HttpFun : public QObject
{
    Q_OBJECT
public:
    explicit HttpFun(QObject *parent = 0);
    void sendRequest(const QString& strUrl);//根据url发起http请求
signals:
    void signal_requestFinished(bool bSuccess,const QString& strResult); //http请求结束
public slots:
    void slot_requestFinished(); //http请求结束
    void slot_requestTimeout();  //请求超时

private:
    QNetworkAccessManager* m_pNetworkManager;//网络管理类
    QNetworkReply* m_pNetworkReply; //封装请求返回信息
    QTimer* m_pTimer; //请求超时计时器
    QString m_strUrl; //记录当前请求的url
};

#endif // HTTPFUN_H

HttpFun.cpp

#include "HttpFun.h"
#include 

const int nHTTP_TIME = 10000; //10秒

HttpFun::HttpFun(QObject *parent) :
    QObject(parent)
{
    m_pNetworkManager = new QNetworkAccessManager(this);
    m_pNetworkReply = NULL;
    m_pTimer = new QTimer;
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(slot_requestTimeout()));//超时信号
}

//发起请求
void HttpFun::sendRequest(const QString &strUrl)
{
    m_strUrl = strUrl;
    QNetworkRequest netRequest;
    netRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    netRequest.setUrl(QUrl(strUrl)); //地址信息
    if(strUrl.toLower().startsWith("https"))//https请求,需ssl支持(下载openssl拷贝libeay32.dll和ssleay32.dll文件至Qt bin目录或程序运行目录)
    {
        QSslConfiguration sslConfig;
        sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
        sslConfig.setProtocol(QSsl::TlsV1_1);
        netRequest.setSslConfiguration(sslConfig);
    }
//    QString strBody; //http body部分,可封装参数信息
//    QByteArray contentByteArray = strBody.toLatin1();//转成二进制
//    m_pNetworkReply = m_pNetworkManager->post(netRequest,contentByteArray);//发起post请求

    m_pNetworkReply = m_pNetworkManager->get(netRequest); //发起get请求
    connect(m_pNetworkReply,SIGNAL(finished()),this,SLOT(slot_requestFinished())); //请求完成信号
    m_pTimer->start(nHTTP_TIME);
}

//请求结束
void HttpFun::slot_requestFinished()
{
    m_pTimer->stop();//关闭定时器
    QByteArray resultContent = m_pNetworkReply->readAll();
    QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
    QString strResult = pCodec->toUnicode(resultContent);
    int nHttpCode = m_pNetworkReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();//http返回码
    if(nHttpCode == 200)//成功
    {
        emit signal_requestFinished(true,strResult);//请求成功
    }
    else
    {
        emit signal_requestFinished(false,strResult);//请求失败
    }
    m_pNetworkReply->deleteLater();
    this->deleteLater(); //释放内存
}

//请求超时
void HttpFun::slot_requestTimeout()
{
    emit signal_requestFinished(false,"timeout");//请求失败
    m_pNetworkReply->deleteLater();
    this->deleteLater();//释放内存
}

使用方法:

//请求按钮点击
void MainDialog::slot_requestBtnClicked()
{
    HttpFun* pHttpFun = new HttpFun();
    QString strUrl = ui->addressEdit->text().trimmed();
    connect(pHttpFun,SIGNAL(signal_requestFinished(bool,const QString&)), //http请求结束信号
            this,SLOT(slot_requestFinished(bool,const QString&)));
    pHttpFun->sendRequest(strUrl);
}

//请求结束
void MainDialog::slot_requestFinished(bool bSuccess, const QString &strResult)
{
    if(bSuccess)
    {
        ui->resultEdit->setPlainText(strResult); //纯文本显示
    }
    else
    {
        ui->resultEdit->setPlainText(tr("请求失败!"));
    }

}


演示程序下载地址

http://download.csdn.net/detail/lingyun0/8245937


By Lankin

2014/12/10


你可能感兴趣的:(Qt,C++)