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"

自己封裝的庫,里面自動多線程解析消息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM