Qt开发http server,兼容Qt4(QtWebApp)
作者:互联网
下载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