簡體   English   中英

QT 中使用 QNetworkAccessManager 的 HTTP POST 請求

[英]HTTP POST request in QT using QNetworkAccessManager

我必須向我的服務器發送一個數據字符串我正在拼命嘗試使用 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;
}

看來你不知道以下幾個概念:

  • 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();
}

主程序

#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#&11397002170466466466466466&12208004230370336441514488!~~~0 ,這似乎是沒有很好地形成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM