• httplib库的安装以及使用


    目录

    安装httplib库

    认识httplib库

    httplib请求类

     httplib响应类

     httplib中的Server类       

    httplib的client类

    httplib库搭建简单的服务器

    httplib库搭建简单的客户端


    安装httplib库

    1.进入github,搜索httplib库

     2.下载库

    3.下载完毕将库的压缩包传输到linux系统下

     4.解压库

    unzip cpp-httplib-master.zip

    认识httplib库

    • httplib 库,一个 C++11 单文件头的跨平台 HTTP/HTTPS 库。
    • httplib 库实际上是用于搭建一个简单的 http 服务器或者客户端的库,这种第三方网络库,可以让我们免去搭建服 务器或客户端的时间,把更多的精力投入到具体的业务处理中,提高开发效率

    httplib请求类

    • httplib请求类是它包含了http请求中的请求行的请求方法,url,协议版本以及请求报头中的各个响应报头值
    1. namespace httplib{
    2. //文件信息结构体
    3. struct MultipartFormData {
    4.        std::string name; //字段名称
    5.        std::string content; //文件内容
    6.        std::string filename; //文件名称
    7.        std::string content_type;//文件类型
    8.   };
    9. using MultipartFormDataItems = std::vector;
    10.    struct Request {
    11.        std::string method; //请求方法
    12.        std::string path; //资源路径
    13.        Headers headers; //请求报头
    14.        std::string body; //请求正文
    15.        // for server
    16.        std::string version; //协议版本
    17.        Params params; //查询字符串
    18.        MultipartFormDataMap files;//保存的是客户端上传的文件信息
    19.        Ranges ranges; //
    20. //判断请求报头中有没有 某个字段
    21.        bool has_header(const char *key) const;
    22. //获取请求报头中对应的字段值
    23.        std::string get_header_value(const char *key, size_t id = 0) const;
    24. //将key-val的字段值设定在http请求中
    25.        void set_header(const char *key, const char *val);
    26. //判定是否有对应的文件
    27.        bool has_file(const char *key) const;
    28. //获取对应的文件信息
    29.        MultipartFormData get_file_value(const char *key) const;
    30.   };

     httplib响应类

    httplib类是将响应行,响应报头,响应正文设定到Response类的对象中,然后将Response对象组织成http响应的形式发送给对方。

    1.    struct Response {
    2.        std::string version; //协议版本号,默认时http1.1
    3.        int status = -1; //响应状态码,
    4.        std::string reason;
    5.        Headers headers; //响应报头
    6.        std::string body; //响应正文
    7.        std::string location; // 重定向位置
    8. //以key-val将相应的字段设定到响应报头中
    9. void set_header(const char *key, const char *val);
    10.        void set_content(const std::string &s, const char *content_type);
    11.   };

     httplib中的Server类       

    1. class Server {
    2. //Handler一个函数指针名称,它的参数是Request,和Response
    3.        using Handler = std::function<void(const Request &, Response &)>;
    4. //Handlers是一个映射表,它映射对应的请求资源和处理函数映射在一起
    5.        using Handlers = std::vector>;
    6. //将Get方法的请求资源与处理函数加载到Handlers表中
    7.        Server &Get(const std::string &pattern, Handler handler);
    8. Server &Post(const std::string &pattern, Handler handler);
    9.        Server &Put(const std::string &pattern, Handler handler);
    10. Server &Patch(const std::string &pattern, Handler handler);  
    11. Server &Delete(const std::string &pattern, Handler handler);
    12. Server &Options(const std::string &pattern, Handler handler);
    13. //线程池
    14.        std::functionvoid)> new_task_queue;
    15. //搭建并启动http
    16.        bool listen(const char *host, int port, int socket_flags = 0);
    17. };

    Handler表结构:

     如上面的Handlers表中:

    • 如果http请求的请求方法是GET方法,且请求资源是/hello,那么服务器则会调用Hello函数,构建相对应的http响应。
    • 如果http请求的请求方法是POST方法,且请求资源是/Post,那么服务器则会调用Post函数. 构建相对应的http响应。
    • 如果http请求中的请求方法和请求资源中 只要有一个不存在Handlers表中或者不符合在Handlers表中,则http服务器会返回一个404的http响应。
    • Get("/hello",Hello):将请求方为GET,请求资源/hello,与函数Hello 注册 在Handlers表中
    • Post("/post",Post):将请求方法POST,请求资源/post,与函数Post 注册 在Handlers表中

    线程池的工作

    • 当服务器收到一个http请求的时候,就会将该http请求放入到线程池中,线程池中的线程就会调用相对应的函数解析http请求形成Request类。
    • 在Handler映射表中,如果有相对应请求方法和请求资源有相对应的映射函数,则会调用相对应的映射函数,并构建好http响应.
    • 当处理函数调用完后,则将构建好的http响应发送给客户端。

    httplib的client类

    1.    class Client {
    2. //构造一个客户端对象,传入服务器Ip地址和端口
    3.        Client(const std::string &host, int port);
    4. //向服务器发送GET请求
    5. Result Get(const char *path, const Headers &headers);
    6. //向服务器发送Post请求
    7. //path是路径,body是request请求路径
    8. //content_length是正文大小
    9. //content_type是正文的类型
    10.        Result Post(const char *path, const char *body, size_t content_length,
    11.              const char *content_type);
    12. //以Post方法上传文件
    13.        Result Post(const char *path, const MultipartFormDataItems &items);
    14.   }

    httplib库搭建简单的服务器

    搭建服务器的步骤:

    • 利用httplib库定义server对象
    • 在Handler表结构中注册 请求资源的处理函数
    • 用listen启动服务器
    1. #include
    2. #include"httplib.h"
    3. using namespace std;
    4. W>void Hello(const httplib::Request& req,httplib::Response& res){
    5. res.set_content("Hello world","text/plain");
    6. res.status=200;
    7. }
    8. void File(httplib::Request req,httplib::Response res){
    9. //获取字段为file的文件
    10. const auto& file=req.get_file_value("file");
    11. std::cout<
    12. std::cout<
    13. }
    14. int main(){
    15. httplib::Server server;//定义sercer对象
    16. //注册处理函数
    17. server.Get("/hi",Hello);
    18. server.Post("/file",File);
    19. server.listen("0.0.0.0",8081);//启动服务器
    20. return 0;
    21. }

     

    注意:

    •  注册处理函数中的Request参数需要定义为const
    • 注册处理函数中Request和Response参数都需要是引用,否则浏览器会接受不到正文内容.

    httplib库搭建简单的客户端

    1. #include"httplib.h"
    2. #define IP "119.23.41.13"
    3. #define PORT 8081
    4. using namespace std;
    5. int main(){
    6. //建立客户端对象
    7. httplib::Client client(IP,PORT);
    8. //单个文件信息组织
    9. httplib::MultipartFormData file;
    10. file.name="file";
    11. file.content="hello world";
    12. file.filename="Hello.txt";
    13. file.content_type="text/plain";
    14. //MultipartFormDataItems对象可以存储多个文件信息
    15. httplib::MultipartFormDataItems item;
    16. item.push_back(file);
    17. //请求服务器上/file资源,发送item文件集合给服务器
    18. auto result=client.Post("/file",item);
    19. return 0;
    20. }

     

     

     

  • 相关阅读:
    传奇攻城期间禁止玩家下地图打怪的脚本写法
    2023最新版JavaSE教程——第7天:面向对象编程(进阶)
    学生成绩管理系统——JAVA
    力扣(300,674)补9.11
    AttributeError: ‘bytes‘ object has no attribute ‘encode‘异常解决方案
    将 .NET Aspire 部署到 Kubernetes 集群
    WiFi基础学习到实战(三:WiFi网络“物理层”)
    类和对象续
    python 中导出requirements.txt 的几种方法
    Springcloud------Nacos&Ribbon&OpenFeign or Eureka
  • 原文地址:https://blog.csdn.net/sjp11/article/details/127990377