当前位置: 首页 > news >正文

c++ http第一个服务

c++ http第一个服务

一、下载相关依赖:这是一个git开源项目 代码仓地址
二、演示代码,编译参数:g++ test.cpp -I/**** -lpthread

#include <httplib.h>
using namespace httplib;void wuhan(const Request &req, Response &res)
{printf("httplib server recv a req: %s\n ", req.path.c_str() );res.set_content("<html>  \<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"> \<h1> 武汉, 加油!</h1></html>","text/html");res.status = 200;
}int main(void)
{using namespace httplib;Server svr;svr.set_base_dir("./");/// Get
svr.Get("/wuhan", wuhan);svr.Get("/hi", [](const Request& req, Response& res) {res.set_content("Hello World!", "text/plain");});svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {auto numbers = req.matches[1];res.set_content(numbers, "text/plain");});svr.Get("/body-header-param", [](const Request& req, Response& res) {if (req.has_header("Content-Length")) {auto val = req.get_header_value("Content-Length");}if (req.has_param("key")) {auto val = req.get_param_value("key");}res.set_content(req.body, "text/plain");});svr.Get("/stop", [&](const Request& req, Response& res) {svr.stop();});/// listensvr.listen("localhost", 1234);
}

三、访问网址http://localhost:1234/hi

http://www.lryc.cn/news/285712.html

相关文章:

  • 深入Android S (12.0) 探索Framework之输入子系统InputReader的流程
  • 【cucumber】cluecumber-report-plugin生成测试报告
  • 华为欧拉操作系统结合内网穿透实现固定公网地址SSH远程连接
  • 加速 Selenium 测试执行最佳实践
  • c语言野指针
  • 【MySQL】where和having的区别
  • npm pnpm yarn 报错或常见问题处理集锦
  • 【Git】常用的Git操作集合
  • JavaScript库jquery的使用方法
  • Vue (v-bind指令、el与data的两种写法、理解MVVM、数据代理、V-no事件处理、双向数据绑定V-model、登陆页面实现
  • SpringBoot - SpringBoot手写模拟SpringBoot启动过程
  • 40. 组合总和 II - 力扣(LeetCode)
  • 第15届蓝桥杯嵌入式省赛准备第二天总结笔记(使用STM32cubeMX创建hal库工程+按键输入)
  • 【论文阅读】One For All: Toward Training One Graph Model for All Classification Tasks
  • Python多线程爬虫——数据分析项目实现详解
  • unity全局音量管理/全局音量设置与音量设置界面(含静音功能)
  • C++ vector 数组转换、查找、最大最小值、排序、排行的几种用法
  • vmware 安装Rocky-9.3系统
  • C++提高编程——模板
  • 单线程、同步、异步、预解析、作用域、隐式全局变量、对象创建、new
  • 《设计模式的艺术》笔记 - 外观模式
  • sql 查询时间范围内的数据
  • TestNG中的@BeforeSuite注释
  • [学习笔记]刘知远团队大模型技术与交叉应用L3-Transformer_and_PLMs
  • 图像处理工具包Pillow的使用分享
  • python进程间通信——命名管道(Named Pipe、FIFO)
  • 03 OSPF 学习大纲
  • HJ7 取近似值【C语言】
  • php基础学习之常量
  • 2024最新面试经验分享