阅读量:105
httplib是一个用于创建HTTP服务器和客户端的C++库。它提供了简单的API,可以方便地处理HTTP请求和响应。
使用httplib库创建HTTP服务器的示例代码如下:
#include
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
int main() {
httplib::Client cli("http://localhost:1234");
auto res = cli.Get("/hello");
if (res && res->status == 200) {
std::cout << res>body << std class="hljs-keyword">return 0;
}
通过上述示例代码可以看出,httplib库提供了简单易用的API,可以轻松地创建HTTP服务器和客户端,处理HTTP请求和响应。