最简单的http服务器传输一个字符串“Hello http!\n”
(1)httpserver.cpp
#include "stdafx.h"
#include <iostream>
#include <chrono>
#include <cstdio>
#include "httplib.h"
using namespace httplib;
int main()
{
Server svr;
if (!svr.is_valid())
{
printf("server has an error...\n");
return -1;
}
svr.Get("/hi", [](const Request& /*req*/, Response& res)
{
res.set_content("Hello http!\n", "text/plain");
});
svr.Get("/stop",
[&](const Request& /*req*/, Response& /*res*/) { svr.stop(); });
svr.listen("localhost", 1234);
}
(2)httpclient.cpp
#include "stdafx.h"
#include <iostream>
#include "httplib.h"
#include <iostream>
using namespace std;
int main()
{
httplib::Client cli("localhost", 1234);
if (auto res = cli.Get("/hi"))
{
cout << res->status << endl;
cout << res->get_header_value("Content-Type") << endl;
cout << res->body << endl;
}
else
{
}
}