用qt接收局域网内http请求的信息

用qt接收局域网内http请求的信息

#之前做过一个小项目需要用qt接收局域网内的http请求信息,折腾了好几天终于做好了,详情可以参考sky_hanlei的这篇文章

其实http协议底层就是基于tcp的,详情可以自行百度。
在.pro文件中加入network模块

MainWindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QJsonDocument>
#include <QTcpServer>
#include <QTcpSocket>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_btn_send_clicked(); //用来发送http Post请求的按钮
    void newConnectSlot();      //建立连接的槽函数
    void tcpSocketSlot();       //读取数据的槽函数

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *m_netAccMg = nullptr;
    QNetworkRequest m_send;
    QJsonDocument sendidenmsgjsondoc;
    QByteArray sendmsg;
    QTcpServer *m_tcpServer;
    QTcpSocket *m_tcpSocket;

};
#endif // MAINWINDOW_H
Mainwindow.cpp文件
其中btn_send是一个QPush_button
show_text是一个text_browser
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QJsonObject>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_netAccMg = new QNetworkAccessManager(this);
    m_send = QNetworkRequest(QUrl("http://192.168.100.89:1234/massage"));   //设置请求地址,别忘了http://!!!
    m_send.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");   //设置请求头格式及数据发送格式
    m_tcpServer = new QTcpServer(this);
    m_tcpSocket = new QTcpSocket(this);
    m_tcpServer->listen(QHostAddress::Any,1234);    //建立监听
    connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(newConnectSlot()));


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btn_send_clicked()
{
    QJsonObject identitymsg;
    identitymsg.insert("Name","xiaoming");
    QJsonObject onmessage;
    onmessage.insert("sex","man");
    identitymsg.insert("Data",onmessage);
    sendidenmsgjsondoc.setObject(identitymsg);
    sendmsg = sendidenmsgjsondoc.toJson(QJsonDocument::Compact);
    m_netAccMg->post(m_send,sendmsg);
}
void MainWindow::newConnectSlot()
{
    m_tcpSocket = m_tcpServer->nextPendingConnection();
    connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(tcpSocketSlot()));
}

void MainWindow::tcpSocketSlot()
{
    QByteArray ary;
    ary = m_tcpSocket->readAll();
    qDebug() << ary << endl;
    QString b = QString(ary);
    ui->text_show->setText(ary);

}

当我们按下send按钮就会发送一个post请求,发送一个嵌套的json数据,然后就会收到并打印出来。
下面这是打印出来的结果,具体的内容大家可以自己参照http协议自行分析并解析

"POST /massage HTTP/1.1\r\nHost: 192.168.100.89:1234\r\nContent-Type: application/json\r\nContent-Length: 40\r\nConnection: Keep-Alive\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: zh-CN,en,*\r\nUser-Agent: Mozilla/5.0\r\n\r\n" 

"{\"Data\":{\"sex\":\"man\"},\"Name\":\"xiaoming\"}" 

下面这个是界面显示截图
在这里插入图片描述

参考连接:https://blog.csdn.net/hl125695339/article/details/8255688

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值