server端代码
#include "httplib.h"
void deal(const Request &req, Response &res);
void deal(const Request &req, Response &res)
{
}
int main()
{ Http_server *p_http_server = (Http_server *)p_param;
const char *p_ip = 127.0.0.1;
int port = 8080;
Server svr;
//处理Get类型主动查询去请求
svr.Get("/Alive_Check", [](const Request& req, Response& res) {
//std::string content_type=req.get_header_value("Content-Type");
std::string version=req.get_header_value("Version");
res.status=HTTP_OK;//设置code码
res.set_content("", "");//设置content—type
res.set_header("Content-Length", "0");//设置content—length
res.set_header("Version", version.c_str());//设置版本
string buf = GMTStrFromTime();
res.set_header("Date", buf.c_str());//设置时间
});
//处理Post类型主动查询去请求
svr.Post("/Alive_Check", [&](const Request& req, Response& res) {
//std::string content_type=req.get_header_value("Content-Type");
std::string version=req.get_header_value("Version");
res.status=HTTP_OK;//设置code码
res.set_content("", "");//设置content—type
res.set_header("Content-Length", "0");//设置content—length
res.set_header("Version", version.c_str());//设置版
string buf = GMTStrFromTime();
res.set_header("Date", buf.c_str());//设置时间
});
//处理更新请求
svr.Get("/update", deal);
svr.Post("/update", deal);
//主动关闭
svr.Get("/stop", [&](const Request& req, Response& res) {
svr.stop();
});
svr.listen(p_ip, port);
return NULL;
}
client端
#include "httplib.h"
int main()
{
Client cli("127.0.0.1", 8080);
string body = "xxxxxxxxxxxxxxxxxxxxxxxx";
Headers headers;
headers.emplace("Host", "xxxxxxxxxxxxxxxxx");
if(auto res = cli.Post("/statusreport", headers, body,"application/xml; charset=utf-8"))
{
if (res->status == HTTP_OK)
{
}
else
{
std::cout << res->status << std::endl;
}
}
else
{
auto err = res.error();
std::cout << "---------->http client error code " << err << std::endl;
}
cli.stop();
return 0;
}
编译链接httplib.h就可以了,注意需要将httplib.h中的正则表达式修改成boost或者升级g++