意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

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

来源:恒创科技 编辑:恒创科技编辑部
2024-05-20 14:04:09

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客户端的示例代码如下:


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

#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请求和响应。

上一篇: 怎么使用python编写简单鸡兔同笼程序 下一篇: oracle怎么添加多个字段