5.QT中关于HTTPClient相关的操作,Json数据传输

  • Post author:
  • Post category:其他






  1. 新建项目


    T12HttpClient









































































T12HttpClient.pro




SOURCES




+=


\







main.cpp





QT




+=


network



CONFIG




+=


C++11





main.cpp


#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
 
int main(int argc,char* argv[])
{
    QCoreApplication app(argc, argv);
    QNetworkAccessManager* manager = new QNetworkAccessManager;
 
QNetworkRequest req;
//这里,我访问的是tomcat服务器
    req.setUrl(QUrl("http://localhost:8080/"));
    manager->get(req);
    QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply){
        if(reply->error() != QNetworkReply::NoError)
        {
            qDebug() << "Error:" << reply->errorString();
            return;
        }
        QByteArray buf = reply->readAll();
        qDebug() << "OK:"<< buf;
    });
 
    return app.exec();
}



运行结果:
















Json



数据传输


#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonDocument>
 
/**
 * json 数据传输
 * @brief main
 * @param argc
 * @param argv
 * @return 
 */
int main(int argc,char* argv[])
{
    QCoreApplication app(argc, argv);
    QNetworkAccessManager* manager = new QNetworkAccessManager;
 
    QJsonObject obj;
    obj.insert("username", QString("tuzuoquan"));
    obj.insert("password", QString("tuzuoquan's password"));
 
    QNetworkRequest req;
    req.setUrl(QUrl("http://XXX/test.cgi"));
    manager->post(req, QJsonDocument(obj).toJson());
    QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply){
        if(reply->error() != QNetworkReply::NoError)
        {
            qDebug() << "Error:" << reply->errorString();
            return;
        }
        QByteArray buf = reply->readAll();
        qDebug() << "OK:"<< buf;
    });
 
    return app.exec();
}



关于



cgi



的代码









#include <stdio.h>




#include <unistd.h>








#include <QCoreApplication>




#include <QTcpSocket>




#include <QJsonDocument>




#include <QJsonObject>




#include <QProcessEnvironment>












int main(int argc, char* argv[])




{








printf(“Content-type:text/html\n\n”);








QCoreApplication app(argc, argv);












QProcessEnvironment envir =








QProcessEnvironment::systemEnvironment();












int contentLen = envir.value(“CONTENT_LENGTH”).toInt();








QTcpSocket socket;












socket.setSocketDescriptor(STDIN_FILENO,








QTcpSocket::ConnectedState,








QTcpSocket::ReadOnly);












QByteArray buf;














QObject::connect(&socket, &QTcpSocket::readyRead, [&](){









buf += socket.readAll();








if(buf.size()


== contentLen)








{









QJsonDocument doc = QJsonDocument::fromJson(buf);








QJsonObject obj = doc.object();





#if 1







QString username = obj.value(“username”).toString();








QString password = obj.value(“password”).toString();








printf(“username is %s; password is %s”,








username.toUtf8().data(), password.toUtf8().data());





#else







QSqlTableModel model;








model.setTable(“tuser”);








model.setFilter(“username=’xx'”);








model.select();








if(model.rowCount() == 1)








printf(“Login success”);








else








printf(“Login Error”);





#endif







app.quit();








}








});












return app.exec();





}





上面的程序代码说明,我们可以通过



post



的方式顺便发送



Json



数据给服务器!









版权声明:本文为toto1297488504原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。