使用 QNetworkAccessManager 在 QT 中的 HTTP POST 请求

Posted

技术标签:

【中文标题】使用 QNetworkAccessManager 在 QT 中的 HTTP POST 请求【英文标题】:HTTP POST request in QT using QNetworkAccessManager 【发布时间】:2020-03-28 17:20:18 【问题描述】:

我必须向我的服务器发送一个数据字符串,我正在拼命尝试使用 QNetworkAccessManager,但是几个星期都没有成功,有人可以帮忙!

我的代码如下。

它可以编译,但是当我运行它时,我得到一个空回复。

QT 应用程序输出:我得到一个空回复

当我检查接收文本的服务器上的日志文件时,它是空的。

我尝试使用 POSTMAN,它工作正常。

请检查下图,它显示了我尝试使用 POSTMAN 时的结果

使用 POSTMAN 的结果

我在下面包含了我的代码,请帮忙。

主程序

#include <QCoreApplication>
#include <stdio.h>
#include <string>
#include <QtDebug>
#include <QObject>
#include <httpmanager.h>


QByteArray dataString ="A=##1802201049565471101000000N0526.874000E07256.4380000101035070#&11397002170466466466466466&12208004230370336441514488!~~~0";
using namespace std;

int main(void)

HTTPmanager myHttpManager;

    while(1)
        myHttpManager.postData("A==##1802201049565471101000000N0526.874000E07256.4380000101035070#&11397002170466466466466466&12208004230370336441514488!~~~0");
        Sleep(1000);        
    //main loop ends 

    return 0;


httpmanager.h--------

#ifndef HTTPMANAGER_H
#define HTTPMANAGER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>


class HTTPmanager : public QObject

    Q_OBJECT
public:
    HTTPmanager();
    ~HTTPmanager();
    void postData(QString str);

private:
    QNetworkAccessManager * manager;
    QNetworkRequest         request;
    QNetworkReply *reply;


private slots:
    void replyFinished(QNetworkReply *rep);

;

#endif // HTTPMANAGER_H

httpmanager.cpp-------------

#include "httpmanager.h"

HTTPmanager::HTTPmanager()




HTTPmanager::~HTTPmanager()

    delete  manager; manager = nullptr;


void HTTPmanager::postData(QString str)

    QByteArray data = str.toUtf8();

    QUrl url;
    manager = new QNetworkAccessManager(this);
    url.setScheme("http");
    url.setHost("test.vivcorefmms.com");
    url.setPath("/node/filter_data");
    request.setUrl(url);
    request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRawHeader("Content-Length", QByteArray::number(data.size()));
    //connect(reply, SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    //connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
    //connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    //loop.exec();
    reply = manager->post(request,data);


    QByteArray bts = reply->readAll();
    QString tmp(bts);
    qDebug() << "Reply:"<<tmp;



void HTTPmanager::replyFinished(QNetworkReply *rep)

    QByteArray bts = rep->readAll();
    QString str(bts);
    qDebug() << "Reply:"<<str;


【问题讨论】:

【参考方案1】:

您似乎不知道以下概念:

Qt Network 像所有 Qt 模块一样异步工作,所以不要考虑同步获取信息。

Qt 需要一个事件循环,在这种情况下 QCoreApplication 就足够了。

如果您想执行周期性任务,那么您应该使用 QTimer 而不是带有 Sleep 的 while 循环。

综合以上,解决办法是:

httpmanager.h

#ifndef HTTPMANAGER_H
#define HTTPMANAGER_H

#include <QObject>

class QNetworkReply;
class QNetworkAccessManager;

class HTTPManager : public QObject

    Q_OBJECT
public:
    HTTPManager(QObject *parent=nullptr);
    ~HTTPManager();
    void postData(QString str);
private Q_SLOTS:
    void replyFinished(QNetworkReply *rep);
private:
    QNetworkAccessManager * manager;
    QNetworkReply *reply;
;

#endif // HTTPMANAGER_H

httpmanager.cpp

#include "httpmanager.h"

#include <QNetworkAccessManager>
#include <QNetworkReply>

HTTPManager::HTTPManager(QObject *parent)
    : QObject(parent), manager(new QNetworkAccessManager(this))

    connect(manager, &QNetworkAccessManager::finished,this, &HTTPManager::replyFinished);


HTTPManager::~HTTPManager()



void HTTPManager::postData(QString str)

    QNetworkRequest request;
    QByteArray data = str.toUtf8();

    QUrl url;

    url.setScheme("http");
    url.setHost("test.vivcorefmms.com");
    url.setPath("/node/filter_data");
    request.setUrl(url);
    request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    manager->post(request, data);


void HTTPManager::replyFinished(QNetworkReply *rep)

    QByteArray bts = rep->readAll();
    QString str = QString::fromUtf8(bts);
    qDebug().noquote() << "Reply:"<<str;
    rep->deleteLater();

ma​​in.cpp

#include "httpmanager.h"

#include <QCoreApplication>
#include <QTimer>

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

    QCoreApplication a(argc, argv);
    HTTPManager manager;
    QTimer timer;
    timer.setInterval(1000);
    QObject::connect(&timer, &QTimer::timeout, [&manager]()
       manager.postData("A==##1802201049565471101000000N0526.874000E07256.4380000101035070#&11397002170466466466466466&12208004230370336441514488!~~~0");
    );
    timer.start();
    return a.exec();

Reply: <head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>

这是因为缺少用户代理,因此将其修改为:

request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRawHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36");
manager->post(request, data);

获得以下内容:

Reply: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Database Error</title>
<style type="text/css">

::selection  background-color: #E13300; color: white; 
::-moz-selection  background-color: #E13300; color: white; 

body 
    background-color: #fff;
    margin: 40px;
    font: 13px/20px normal Helvetica, Arial, sans-serif;
    color: #4F5155;


a 
    color: #003399;
    background-color: transparent;
    font-weight: normal;


h1 
    color: #444;
    background-color: transparent;
    border-bottom: 1px solid #D0D0D0;
    font-size: 19px;
    font-weight: normal;
    margin: 0 0 14px 0;
    padding: 14px 15px 10px 15px;


code 
    font-family: Consolas, Monaco, Courier New, Courier, monospace;
    font-size: 12px;
    background-color: #f9f9f9;
    border: 1px solid #D0D0D0;
    color: #002166;
    display: block;
    margin: 14px 0 14px 0;
    padding: 12px 10px 12px 10px;


#container 
    margin: 10px;
    border: 1px solid #D0D0D0;
    box-shadow: 0 0 8px #D0D0D0;


p 
    margin: 12px 15px 12px 15px;

</style>
</head>
<body>
    <div id="container">
        <h1>A Database Error Occurred</h1>
        <p>Error Number: 1048</p><p>Column 'node_id' cannot be null</p><p>INSERT INTO `node_info` (`node_info_id`, `node_id`, `message_id`, `gsm`, `wifi`, `gprs`, `_3g`, `bluetooth`, `gps`, `emergency_code`, `env_temperature`, `env_humidity`, `env_pressure`, `gps_speed`, `gps_latitude`, `gps_longitude`, `date`, `time`, `number_of_compartment`, `number_of_dispense`, `sys_date`, `sys_time`) VALUES ('', NULL, '654', '7', '0', '1', '1', '1', '0', '0', '03', '507', '0101', '0', NULL, NULL, '#18022', '010495', 15, '', '2020-03-28', '23:31:10')</p><p>Filename: models/Model_master.php</p><p>Line Number: 156</p>  </div>
</body>
</html>

消息指出错误:

Error Number: 1048

Column 'node_id' cannot be null

INSERT INTO `node_info` (`node_info_id`, `node_id`, `message_id`, `gsm`, `wifi`, `gprs`, `_3g`, `bluetooth`, `gps`, `emergency_code`, `env_temperature`, `env_humidity`, `env_pressure`, `gps_speed`, `gps_latitude`, `gps_longitude`, `date`, `time`, `number_of_compartment`, `number_of_dispense`, `sys_date`, `sys_time`) VALUES ('', NULL, '654', '7', '0', '1', '1', '1', '0', '0', '03', '507', '0101', '0', NULL, NULL, '#18022', '010495', 15, '', '2020-03-28', '23:18:26')

Filename: models/Model_master.php

Line Number: 156

其中显示发送的数据似乎不符合处理导致的错误,即A==##1802201049565471101000000N0526.874000E07256.4380000101035070#&amp;11397002170466466466466466&amp;12208004230370336441514488!~~~0似乎格式不正确

【讨论】:

以上是关于使用 QNetworkAccessManager 在 QT 中的 HTTP POST 请求的主要内容,如果未能解决你的问题,请参考以下文章

离线使用 QNetworkAccessManager

跨 dll 使用 QNetworkAccessManager

使用 QNetWorkAccessManager 将值传递给插槽

如何使用 Qt/QNetworkAccessManager (C++) 实现 SFTP

如何使用 QNetworkAccessManager 找出数据传输延迟

使用 QNetworkAccessManager 时如何处理代理