当前位置: 首页 > 工具软件 > JSON Form > 使用案例 >

Qt --- http客户端post JSON/form方式

华宏逸
2023-12-01

Qt http客户端的实现

主要是post数据的打包和http的头设置

1.form-data表单方式

    //1.打包form-data表单数据
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart textPart;
    //format
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"format\""));
    textPart.setBody(FORMAT);
    multiPart->append(textPart);
    //charset
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"charset\""));
    textPart.setBody(CHARSET);
    multiPart->append(textPart);
    //dateTime
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"dateTime\""));
    textPart.setBody(curDateTime.toString("yyyy-MM-ddTHH:mm:ss").toLatin1());
    multiPart->append(textPart);
    //sign
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"sign\""));
    textPart.setBody(sign.toLatin1());
    multiPart->append(textPart);

    multiPart->setBoundary("---你的分隔符---");       //设置分隔符,很重要!!!

    //2.构造URL,请求头
    QString Urlstr= "你的URL";
    QUrl url(Urlstr);
    //3构造请求头
    request.setUrl(url);
    //form-data方式
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data;boundary=---你的分隔符---"));
    request.setRawHeader("Connection", "Keep-Alive");

    //4.post数据
    //发送请求
    QNetworkReply *reply = manager->post(request, multiPart);

    multiPart->setParent(reply); // delete the multiPart with the reply

2.x-www-form-urlencoded 表单方式

    //1.打包form表单数据
    QUrlQuery postData;
    postData.addQueryItem("key", QString(key));                              //密钥
    postData.addQueryItem("cmd", QString(CMD));                              //指令类型
    postData.addQueryItem("data", QString("112233"));                        //数据
    ...
    QByteArray data;
    data = postData.toString(QUrl::FullyEncoded).toUtf8();
    qDebug() << data;

    //2.构造请求授权url
    QString urlStr = "https://...你的URL...";
    qDebug() << "URL: " << urlStr;
    QUrl url(urlStr);

    //3.https支持
    QSslConfiguration conf = request.sslConfiguration();
    conf.setPeerVerifyMode(QSslSocket::VerifyNone);
    conf.setProtocol(QSsl::TlsV1_0);
//    conf.setProtocol(QSsl::SslV2);
//    conf.setProtocol(QSsl::TlsV1SslV3);
    request.setSslConfiguration(conf);

    //4.组织https的头
    request.setUrl(url);
    //application/x-www-form-urlencoded
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded;charset=UTF-8"));
    request.setRawHeader("Connection", "Keep-Alive");
    request.setRawHeader("referer", referer.toLatin1());//自定义增加的头

    //5.发送请求
    manager->post(request, data);

3. JSON方式

    QJsonObject json;
    QJsonDocument jsonDoc;

    //1.打包json数据
    json["cmd"] = "CMD";                    //
    json["data"] = "0011";
    ...组织你的JSON串...

    jsonDoc.setObject(json);
    QByteArray dataArray = jsonDoc.toJson(QJsonDocument::Compact);
    qDebug() << "JSON_Data:" << dataArray;

    //2.构造URL,请求头
    QString Urlstr = "http://...你的URL...";
    qDebug() << "Urlstr: " << Urlstr << endl;
    QUrl url(Urlstr);
    //构造请求头
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json;charset=UTF-8"));
    request.setRawHeader("Connection", "Keep-Alive");

    //3.post数据
    //发送请求
//    manager->post(request, dataArray);
    QNetworkReply *psamReply = manager->post(request, dataArray);

接收响应的处理,可以在构造函数中,加入:

    manager = new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotReplyFinished(QNetworkReply*)));

    //打印当前Qt版本支持的Network类型
    qDebug() << manager->supportedSchemes();

 

 类似资料: