欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > cpp http server/client

cpp http server/client

2024/10/25 21:23:38 来源:https://blog.csdn.net/BBJG_001/article/details/140236991  浏览:    关键词:cpp http server/client

httplib

使用httplib库

basedemo

server.cpp

#include "httplib.h"
#include <iostream>
using namespace httplib;int main(void)
{Server svr;svr.Get("/hello", [](const Request& req, Response& res) {std::cout << "log, path=" << req.path << std::endl;auto it = req.params.find("name");std::string name;if(it != req.params.end()){name = it->second;}res.set_content("Hello "+name, "text/plain");});svr.listen("0.0.0.0", 50000);
}

curl一下

$ curl "127.0.0.1:50000/hello?name=zhangsan"
Hello zhangsan%

client.cpp

#include <httplib.h>
#include <iostream>using namespace httplib;int main(void){Client cli("127.0.0.1", 50000);if (auto res = cli.Get("/hello?name=zhangsi")) {std::cout << res->status << std::endl;std::cout << res->get_header_value("Content-Type") << std::endl;std::cout << res->body << std::endl;} else {std::cout << "error code: " << res.error() << std::endl;}return 0;
}

run client

$ g++ tclient.cpp -g -std=c++11 -o tclient && ./tclient
200
text/plain
Hello zhangsi

commondemo

其中struct和json转换可以参见cpp struct json相互转换

server.cpp

#include <unistd.h>
#include "httplib.h"
#include <iostream>
#include <mockutil.h>
using namespace httplib;int main(void)
{Server svr;UserUtil mock_user_util;svr.Get("/hello", [](const Request& req, Response& res) {std::cout << "log, path=" << req.path << std::endl;auto it = req.params.find("name");std::string name;if(it != req.params.end()){name = it->second;}res.set_content("Hello "+name, "text/plain");});svr.Get(R"(/user/(\w+)/get)", [&](const Request& req, Response& res) {auto uname = req.matches[1];User* user = mock_user_util.get(uname.str());std::string ret;if(user==nullptr){ret = "get no user(name="+uname.str()+")";}else{json j;user->to_json(j);ret = j.dump();}res.set_content(ret, "text/plain");});svr.Post(R"(/user/(\w+)/set)", [&](const Request& req, Response& res) {auto uname = req.matches[1];User user;user.from_json(req.body);mock_user_util.set(user);res.set_content("ok", "text/plain");});svr.listen("0.0.0.0", 50001);std::cout << "1111" << std::endl;}

client.cpp

#include <httplib.h>
#include <iostream>
#include "mockutil.h"
using namespace httplib;int main(void){Client cli("127.0.0.1", 50001);auto res = cli.Get("/user/zhangwu/get");User user;user.from_json(res->body);std::cout << "get user, name=" << user.Name << ", Phone=" << user.MPhone.Num << std::endl;user.MPhone.Num = 88888;json j;user.to_json(j);auto res2 = cli.Post("/user/zhangwu/set", j.dump(), "application/json");std::cout << "set user, name=" << user.Name << ", Phone=" << user.MPhone.Num << std::endl;auto res3 = cli.Get("/user/zhangwu/get");User userx;userx.from_json(res3->body);std::cout << "get after set, name=" << userx.Name << ", Phone=" << userx.MPhone.Num << std::endl;return 0;
}

run client

$ g++ tclient.cpp -g -std=c++11 -I./ -o tclient && ./tclient
get user, name=zhangwu, Phone=12345
set user, name=zhangwu, Phone=88888
get after set, name=zhangwu, Phone=88888

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com