c++中httplib库的用法是什么

c++
1335
2024/5/29 20:06:23
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

httplib是一个用于创建HTTP服务器和客户端的C++库。它提供了简单的API,可以方便地处理HTTP请求和响应。

使用httplib库创建HTTP服务器的示例代码如下:

#include <httplib.h>

int main() {
    httplib::Server svr;

    svr.Get("/hello", [](const httplib::Request& req, httplib::Response& res) {
        res.set_content("Hello World!", "text/plain");
    });

    svr.listen("localhost", 1234);

    return 0;
}

使用httplib库创建HTTP客户端的示例代码如下:

#include <httplib.h>

int main() {
    httplib::Client cli("http://localhost:1234");

    auto res = cli.Get("/hello");
    if (res && res->status == 200) {
        std::cout << res->body << std::endl;
    }

    return 0;
}

通过上述示例代码可以看出,httplib库提供了简单易用的API,可以轻松地创建HTTP服务器和客户端,处理HTTP请求和响应。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 使用红黑树解决C++中的大数据排序问题