QT Network Programming: Use QNetworkAccessManager to upload post requests

1. Function introduction

In project development, the device terminal often needs to upload some logs or report status information to the server. The following uses QNetworkAccessManager to encapsulate a function to facilitate the upload of the data to be uploaded. The submitted data format uses JSON format, and the request method uses post. There is no associated status slot function in the code. The amount of data uploaded in actual applications is relatively small, and the data will be repeatedly uploaded intermittently, and the success status is not judged.

Two, the core code

#define TOKEN "abc-def-ghi-jkl-mno"
QString DeviceID="123456789";
QNetworkAccessManager SendErrorInfo_manager;
void SERVER_SendErrorInfo(QString text)
{
    //请求地址
    QString requestUrl;
    QNetworkRequest request;
    //请求地址
    requestUrl="http://192.168.1.123:6666/carmonitorsys/errlog/upload.action?";
    requestUrl+=QString("token=%1&").arg(TOKEN);   //授权码
    requestUrl+=QString("devId=%1").arg(DeviceID); //设备编号

    //设置请求地址
    QUrl url;
    url.setUrl(requestUrl);
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    //打包请求参数赋值
    QJsonObject post_data;
    QJsonDocument document;
    QByteArray post_param;

    post_data.insert("msg",text); //插入数据
    document.setObject(post_data);
    post_param = document.toJson(QJsonDocument::Compact);
   //开始上传
    SendErrorInfo_manager.post(request,post_param);
}

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/108599005