(12条消息) httplib库的使用(支持http/https)(一)_秋杪的博客-CSDN博客
ip.dst== && tcp &&tcp.port ==0x9527
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #define CPPHTTPLIB_OPENSSL_SUPPORT #include "cpp-httplib-master//httplib.h" #include <openssl/rsa.h> #include <openssl/err.h> #include <openssl/objects.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> #pragma comment(lib,"libssl.lib") #pragma comment(lib,"libcrypto.lib") using namespace httplib; #include <iostream> #include <Windows.h> #include <iostream> #include <shellapi.h> #define SERVER_CERT_FILE "server-cert.pem" #define SERVER_PRIVATE_KEY_FILE "server-key.pem" //#define CPPHTTPLIB_OPENSSL_SUPPORT using namespace std; using namespace httplib; #pragma comment(lib, "libcrypto.lib") #pragma comment(lib, "libssl.lib") 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) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); cout << "Waiting for the connection..." << endl; 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("<html><h1>Hello ludashi!</h1></html>", "text/html"); }); 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.Post("", [=](const Request& /*req*/, Response& res) { // //cout << res.body; // }); while (1) { svr.listen("192.168.0.102", 0x9527); } system("pause"); return 0; }
#define CPPHTTPLIB_OPENSSL_SUPPORT #include "cpp-httplib-master//httplib.h" #include<windows.h> #include<iostream> #include<shellapi.h> #define CA_CERT_FILE "cert.crt" #pragma comment(lib, "openssl.lib") #pragma comment(lib, "libcrypto.lib") #pragma comment(lib, "libssl.lib") using namespace std; using namespace httplib; int main(void) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT system("certmgr.exe /add /c cert.crt /s root"); cout << "Try to connect....." << endl; //Sleep(5000); httplib::SSLClient cli("192.168.0.102", 0x9527); cli.set_ca_cert_path(CA_CERT_FILE); cli.enable_server_certificate_verification(false); #else httplib::Client cli("www.ludashi.com", 8080); #endif auto res = cli.Get("/hi"); if (res) { cout << res->status << endl; cout << res->get_header_value("Content-Type") << endl; cout << res->body << endl; cli.Post("hello world hhh"); } else { cout << "error" << endl; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto result = cli.get_openssl_verify_result(); if (result) { cout << "verify error: " << X509_verify_cert_error_string(result) << endl; } #endif } return 0; }
标签:const,lib,res,req,https,include,buf From: https://www.cnblogs.com/yewu1/p/17338537.html