使用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 

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

 

 

 

你可能感兴趣的:(MinGW)