c++ 开发http服务,解析http消息
#include <QCoreApplication> #include "serverhttp.h" #include "msghttp.h" #include <QDebug> #include <QDateTime> class HttpHandle : public LarkinHttp::MsgHandle { public: void doFunction(LarkinHttp::MsgRequest* req, LarkinHttp::MsgResponse* res) override; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); LarkinHttp::ServerHttp server; if(!server.init(8888)){ return 0; } server.setHandleNumber(2); HttpHandle handle; server.applyHandle(&handle); server.workEnable(true); return a.exec(); } void HttpHandle::doFunction(LarkinHttp::MsgRequest *req, LarkinHttp::MsgResponse *res) { qDebug() << "method:" << req->strMethod << " url:" << req->strUrl; res->strCode = "200"; res->strDesc = "OK"; res->strContentType = "text/plain"; res->strBody = QString("[%1] hello").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz")); }
说明:
#include "serverhttp.h"
#include "msghttp.h"
自己封装的库,里面自动多线程解析消息。