zoukankan      html  css  js  c++  java
  • 使用QHttp与C#编写的服务端交互(编译环境mingw)

    打开qtcreator,新建一个项目,然后加一个头文件及源代码文件,如下:

    QtHttp.h:

    #ifndef QTHTTP_H
    #define QTHTTP_H
    #include <Qt/QObject.h>
    #include <Qt/qhttp.h>
    #include <QtCore/QString>
    #include <QtDebug>
    #include <Qt/qbytearray.h>
     
    class QtHttp : QObject {
    Q_OBJECT
    public:
     QtHttp();
     virtual ~QtHttp();
    private:
     QHttp *http;
     QByteArray httptext;
    public slots:
     void on_PushButtonPost_clicked();
     void on_http_stateChanged(int stat);
     void on_http_dataReadProgress(int done, int total);
     void on_http_dataSendProgress(int done, int total);
     void on_http_done(bool error);
     void on_http_requestFinished(int id, bool error);
     void on_http_requestStarted(int id);
     void on_http_responseHeaderReceived(const QHttpResponseHeader &resp);
    };
     
     
    #endif // QTHTTP_H

    QtHttp.cpp:

    #include "QtHttp.h"
    QtHttp::QtHttp() {
        http = new QHttp();
        http->setObjectName("http");
        connect(http, SIGNAL(stateChanged(int)), this, SLOT(on_http_stateChanged(int)));
        connect(http, SIGNAL(dataReadProgress(int, int)), this, SLOT(on_http_dataReadProgress(int, int)));
        connect(http, SIGNAL(dataSendProgress(int, int)), this, SLOT(on_http_dataSendProgress(int, int)));
        connect(http, SIGNAL(done(bool)), this, SLOT(on_http_done(bool)));
        connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(on_http_requestFinished(int, bool)));
        connect(http, SIGNAL(requestStarted(int)), this, SLOT(on_http_requestStarted(int)));
        connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(on_http_responseHeaderReceived(const QHttpResponseHeader &)));
    }
    QtHttp::~QtHttp() {
        delete http;
        http = NULL;
    }
    void QtHttp::on_http_stateChanged(int stat)
    {
        switch (stat)
        {
            case QHttp::Unconnected : qDebug() << "Unconnected\n" ;
                  break;
            case QHttp::HostLookup : qDebug() << "HostLookup\n" ;
                  break;
            case QHttp::Connecting : qDebug() << "Connecting\n" ;
                  break;
            case QHttp::Sending : qDebug() << "Sending\n" ;
                  break;
            case QHttp::Reading : qDebug() << "Reading\n" ;
                  break;
            case QHttp::Connected : qDebug() << "Connected\n" ;
                  break;
            case QHttp::Closing : qDebug() << "Closing\n" ;
                  break;
        }
    }
    void QtHttp::on_http_dataReadProgress(int done, int total)
    {
        qDebug() << "Downloaded " << done << " bytes " << " out of " << total << "\n";
    }
    void QtHttp::on_http_dataSendProgress(int done, int total)
    {
        qDebug() << "Sended " << done << " bytes " << " out of " << total << "\n";
    }
    void QtHttp::on_http_done(bool error)
    {
        if (error)
        {
            qDebug() << http->errorString() << "\n";
        }
        else
        {
            qDebug() << "Session finished successfully\n";
            //buffer.close();
                  httptext = http->readAll();
            qDebug() << "Received Size: " << httptext.count() << "\n";
            QString strSource(httptext);
            qDebug() << httptext;
        }
    }
    void QtHttp::on_http_requestFinished(int id, bool error)
    {
        qDebug() << id <<" Request Finished\n";
        if (error)
        {
            qDebug() << "with errors\n";
            qDebug() << http->errorString() << "\n";
        }
        else
        {
            qDebug() << " successfully \n";
        }
    }
    void QtHttp::on_http_requestStarted(int id)
    {
        qDebug() << "Request Started\n";
    }
    void QtHttp::on_http_responseHeaderReceived(const QHttpResponseHeader &resp)
    {
        qDebug() << "HTTP response header received \n";
    }
    void QtHttp::on_PushButtonPost_clicked()
    {
        QString data("name=你好中国&id=1234567890&addr=asdfghjkl");
        //http.post("http://localhost/biselaw/SalesReport.xml", data, &file);
        /*
        buffer.setData(httptext);
        buffer.open(QIODevice::WriteOnly);
        */
        qDebug() << "Post Data: " << data << "\n";
        http->setHost("localhost");
        //http->post("/biselaw/vote.php", data.toUtf8());
        QHttpRequestHeader header("POST", "/temp/default.aspx") ;
        header.setValue("Host", "localhost") ;
        header.setContentType("application/x-www-form-urlencoded");
        http->request(header, data.toUtf8()) ;
        //QString strBuffer(httptext);
        //TextEditHttp->setHtml(strBuffer);
    }

     注意还需要在qt.pro里加上

    QT += network 

     才能编译,应该是引用库吧

  • 相关阅读:
    PYTHON核心编程第7章 7.5题
    openssl大漏洞
    运维之路
    组件嵌套+Mixin函数demo
    React初识(按钮点击+输入绑定)
    vue切换按钮(关闭消失型)
    动态发表之后的显示时间
    展开全部的实现
    前端进行后台数据的处理
    将项目上传到git上,并在测试服务器上运行
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/2798461.html
Copyright © 2011-2022 走看看