温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Qt如何实现http服务

发布时间:2021-12-03 13:44:04 阅读:4484 作者:小新 栏目:大数据
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

先看执行结果:

Qt如何实现http服务  
Qt HttpServer

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

下面是具体代码:

HttpDemo.pro

QT += coreQT += networkQT -= guiCONFIG += c++11 consoleCONFIG -= 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.0SOURCES += \        main.cpp \        myhttpserver.cpp# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += targetHEADERS += \    myhttpserver.h

myhttpserver.h

#ifndef MYHTTPSERVER_H#define MYHTTPSERVER_H#include <QObject>#include <QTcpServer>#include <QTcpSocket>class MyHttpServer : public QObject{    Q_OBJECTpublic:    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如何实现http服务”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/dotnet9/blog/4442894

AI

开发者交流群×