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