Qt如何实现http服务

这篇文章将为大家详细讲解有关Qt如何实现http服务,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

先看执行结果:

 

Qt HttpServer

左边是开启的Qt Http服务,监控服务端口,及接收客户端请求;右侧是浏览器访问服务。

下面是具体代码:

HttpDemo.pro

QT += core
QT += network
QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp \
        myhttpserver.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    myhttpserver.h

myhttpserver.h

#ifndef MYHTTPSERVER_H
#define MYHTTPSERVER_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>

class MyHttpServer : public QObject
{
    Q_OBJECT
public:
    explicit MyHttpServer(QObject *parent = nullptr);
    ~MyHttpServer();

    QTcpSocket *socket;

public slots:
    void connection();

private:
    qint64 bytesAvailable() const;
    QTcpServer *server;

signals:

public slots:
};

#endif // MYHTTPSERVER_H

main.cpp

#include <QCoreApplication>
#include "myhttpserver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyHttpServer server;

    return a.exec();
}

myhttpserver.cpp

#include "myhttpserver.h"
#include <QDebug>

MyHttpServer::MyHttpServer(QObject *parent) : QObject(parent)
{
    server = new QTcpServer(this);
    connect(server, &QTcpServer::newConnection, this, &MyHttpServer::connection);
    if(!server->listen(QHostAddress::Any, 8080))
    {
        qDebug() << "\nWeb服务未启动";
    }
    else
    {
         qDebug() << "\nWeb服务在端口8080等待客户端连接";
    }
}

void MyHttpServer::connection()
{
    socket = server->nextPendingConnection();

    while (!(socket->waitForReadyRead(100))); //等待从浏览器读取数据

    char webBrowerRXData[1000];
    int sv = socket->read(webBrowerRXData, 1000);
    qDebug() << "正在从浏览器读取数据=" << QString(webBrowerRXData);

    socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before   \n
    socket->write("Content-Type: text/html\r\n");
    socket->write("Connection: close\r\n");
    socket->write("Refresh: 1\r\n\r\n");        //refreshes web brower   every second.Request

    socket->write("<!DOCTYPE>"
                  "<html>"
                      "<header>"
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
                        "<title>测试Qt HttpServer</title>"
                      "</header>"
                      "<body>已连接秒数:");
    QByteArray str;
    static qint16 count;    //用于在浏览器上显示的统计数字
    str.setNum(count++);
    socket->write(str);
    socket->write("</body>"
                  "</html>");

    socket->flush();
    connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
    socket->disconnectFromHost();

}


MyHttpServer::~MyHttpServer()
{
    socket->close();
}

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值