cout << "Server is running at http://localhost:8080" << endl;
} else {
cerr << "Failed to start server." << endl;
}
return 0;
}
复制代码
发送 HTTP 请求
#include "httplib.h"
using namespace std;
using namespace httplib;
int main() {
Client cli("httpbin.org");
auto res = cli.Get("/get");
if (res && res->status == 200) {
cout << "Response body:" << endl;
for (auto &line : res->body) {
cout << line << endl;
}
} else {
cerr << "Request failed!" << endl;
}
return 0;
}
复制代码
结论
cpp-httplib 提供了一个高效、轻量级的解决方案,用于实现 C++ 中的 HTTP 和 HTTPS 功能。无论您必要创建 RESTful API 服务还是在您的应用步伐中与其他 Web 服务进行交互,cpp-httplib 都是一个值得尝试的选择。立即参加并开始使用吧!
项目堆栈地址:https://gitcode.com/yhirose/cpp-httplib