通过QHttpServer获取请求url

问题描述 投票:0回答:1

下面是一个使用QHttpServer的简单示例。

#include <QtCore>
#include <QtHttpServer>

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

    QHttpServer httpServer;
    httpServer.route("/", []() {
        return "Hello world";
    });
    httpServer.route("/test/", []() {
        return "test";
    });

    httpServer.afterRequest([](QHttpServerResponse &&resp) {
        resp.setHeader("Server", "Super server!");
        return std::move(resp);
    });

    const auto port = httpServer.listen(QHostAddress::Any);
    if (!port) {
        qDebug() << QCoreApplication::translate(
                "QHttpServerExample", "Server failed to listen on a port.");
        return 0;
    }

    qDebug() << QCoreApplication::translate(
            "QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);

    return app.exec();
}

运行代码后,您可以在浏览器中输入以下网址,

http://127.0.0.1:端口

世界你好

http://127.0.0.1:端口/测试

测试

我的目的是在一个函数中捕获所有请求——即使是任意子网址,例如

http://127.0.0.1:port/abc

http://127.0.0.1:port/zzz/test/123123

我如何获取这些网址?

我向AI询问了此事。它指向

QHttpRequest
,但我的 Qt 库中没有这样的东西。而且我也查了Qt文档,不存在。

c++ qt http network-programming
1个回答
0
投票

感谢@m7913d的提醒。我发现了这个,

server.setMissingHandler([](const QHttpServerRequest& request,
    QHttpServerResponder&& responder) {
        request.url();
    });
© www.soinside.com 2019 - 2024. All rights reserved.