ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Qt开发http server,兼容Qt4(QtWebApp)

2021-09-18 17:04:50  阅读:393  来源: 互联网

标签:CHttpServer Qt4 http Qt QtWebApp app write include response


下载QtWebApp

http://www.stefanfrings.de/qtwebapp/QtWebApp.zip


解压后将httpserver中的文件引入到项目中
编写CHttpServer类,继承自HttpRequestHandler,并重新实现service接口,代码如下:

CHttpServer.h

#ifndef CHTTPSERVER_H
#define CHTTPSERVER_H

#include <QObject>
#include "QtWebApp/httpserver/httprequesthandler.h"

using namespace stefanfrings;

class CHttpServer : public HttpRequestHandler
{
    Q_OBJECT
public:
    explicit CHttpServer(QObject *parent = 0);
    
    void service(HttpRequest& request, HttpResponse& response);
signals:
    
public slots:
    
};

#endif // CHTTPSERVER_H

CHttpServer.cpp

#include "chttpserver.h"

CHttpServer::CHttpServer(QObject *parent) :
    HttpRequestHandler(parent)
{

}

void CHttpServer::service(HttpRequest& request, HttpResponse& response)
{
    // Get a request parameters
    QByteArray username=request.getParameter("username");
    QByteArray path = request.getPath();

    // Set a response header
    response.setHeader("Content-Type", "text/html; charset=UTF-8");

    // Generate the HTML document
    response.write("<html><body>");
    response.write("Hello ");
    response.write(username);
    response.write(" path="+path);
    response.write("</body></html>");
}

main.cpp

#include "widget.h"
#include <QApplication>
#include <chttpserver.h>
#include <QDebug>

#include "QtWebApp/httpserver/httplistener.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Widget w;
    w.show();

    new HttpListener(
        new QSettings("E:/test/test1/cfg.ini",QSettings::IniFormat,&app),
        new CHttpServer(&app),
        &app);
    
    return app.exec();
}

cfg.ini为配置文件,其内容如下:

;host=192.168.0.100
port=8088
minThreads=4
maxThreads=100
cleanupInterval=60000
readTimeout=60000
maxRequestSize=16000
maxMultiPartSize=10000000
;sslKeyFile=ssl/my.key
;sslCertFile=ssl/my.cert

运行程序后访问:

http://127.0.0.1:8080/path?username=test

浏览器显示: 

标签:CHttpServer,Qt4,http,Qt,QtWebApp,app,write,include,response
来源: https://blog.csdn.net/Think88666/article/details/120370470

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有