cpp-httplib的链接地址:
https://github.com/yhirose/cpp-httplib/blob/master/example/benchmark.cc
此http库使用简单,只需要包含httplib.h头文件就可以使用,工程引入十分方便。例子如下:
client.cpp
#include <httplib.h> #include <iostream> using namespace std; int main(void) { httplib::Client cli("localhost", 8080); httplib::Params pm; pm.insert(std::make_pair("userMrn", "sunxiang")); pm.insert(std::make_pair("password", "123456")); auto res = cli.Post("/services/usermanage/v1/loginUser", pm); if (res) { cout << res->status << endl; cout << res->get_header_value("Content-Type") << endl; cout << res->body << endl; } return 0; }
server.cpp
// // sample.cc // // Copyright (c) 2019 Yuji Hirose. All rights reserved. // MIT License // #include <chrono> #include <cstdio> #include <httplib.h> #define SERVER_CERT_FILE "./cert.pem" #define SERVER_PRIVATE_KEY_FILE "./key.pem" using namespace httplib; std::string dump_headers(const Headers &headers) { std::string s; char buf[BUFSIZ]; for (auto it = headers.begin(); it != headers.end(); ++it) { const auto &x = *it; snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str()); s += buf; } return s; } std::string log(const Request &req, const Response &res) { std::string s; char buf[BUFSIZ]; s += "================================\n"; snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), req.version.c_str(), req.path.c_str()); s += buf; std::string query; for (auto it = req.params.begin(); it != req.params.end(); ++it) { const auto &x = *it; snprintf(buf, sizeof(buf), "%c%s=%s", (it == req.params.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str()); query += buf; } snprintf(buf, sizeof(buf), "%s\n", query.c_str()); s += buf; s += dump_headers(req.headers); s += "--------------------------------\n"; snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str()); s += buf; s += dump_headers(res.headers); s += "\n"; if (!res.body.empty()) { s += res.body; } s += "\n"; return s; } int main(void) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); #else Server svr; #endif if (!svr.is_valid()) { printf("server has an error...\n"); return -1; } svr.Get("/", [=](const Request & /*req*/, Response &res) { res.set_redirect("/hi"); }); svr.Get("/hi", [](const Request & /*req*/, Response &res) { res.set_content("Hello World!\n", "text/plain"); }); svr.Get("/slow", [](const Request & /*req*/, Response &res) { std::this_thread::sleep_for(std::chrono::seconds(2)); res.set_content("Slow...\n", "text/plain"); }); svr.Get("/dump", [](const Request &req, Response &res) { res.set_content(dump_headers(req.headers), "text/plain"); }); svr.Get("/stop", [&](const Request & /*req*/, Response & /*res*/) { svr.stop(); }); svr.set_error_handler([](const Request & /*req*/, Response &res) { const char *fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>"; char buf[BUFSIZ]; snprintf(buf, sizeof(buf), fmt, res.status); res.set_content(buf, "text/html"); }); svr.set_logger([](const Request &req, const Response &res) { printf("%s", log(req, res).c_str()); }); svr.listen("localhost", 8080); return 0; }
Base64库的下载地址:
https://download.csdn.net/download/qq_23350817/12548393
此库是加密/解密库,使用起来非常方便,直接调用b64_encode()和b64_decode()就可以对数据进行加密/解密。
Base64用法例子:
#include <b64.h> #include <string> #include <vector> #include <iostream> int main() { std::string password = "123456"; std::vector<unsigned char> chs(password.begin(), password.end()); std::cout << b64_encode(chs.data(), chs.size()); while(1); return 0; }