helloworld.cpp 787 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "helloworld.h"
  2. #include <QCoreApplication>
  3. #include <qhttpserver.h>
  4. #include <qhttprequest.h>
  5. #include <qhttpresponse.h>
  6. /// HelloWorld
  7. HelloWorld::HelloWorld()
  8. {
  9. QHttpServer *server = new QHttpServer(this);
  10. connect(server, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)),
  11. this, SLOT(handleRequest(QHttpRequest*, QHttpResponse*)));
  12. server->listen(QHostAddress::Any, 8080);
  13. }
  14. void HelloWorld::handleRequest(QHttpRequest *req, QHttpResponse *resp)
  15. {
  16. Q_UNUSED(req);
  17. QByteArray body = "Hello World";
  18. resp->setHeader("Content-Length", QString::number(body.size()));
  19. resp->writeHead(200);
  20. resp->end(body);
  21. }
  22. /// main
  23. int main(int argc, char **argv)
  24. {
  25. QCoreApplication app(argc, argv);
  26. HelloWorld hello;
  27. app.exec();
  28. }