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


  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数据给服务器!

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。