目录
背景:项目需要采用http+json的架构来与平台通信,在开源库的选取上选择头文件only的httplib和rapidjson,采用post方式进行请求,以下写了一个小demo来调试,其中包含了json的序列化与反序列化。编译环境Ubuntu16.04 ,直接写的测试demo,g++ 文件 -std=c++11 -pthread 即可。
开源库:
1.httplib https://github.com/yhirose/cpp-httplib
2.rapidjson https://github.com/Tencent/rapidjson
代码:
client.cpp
#include "httplib.h"
#include "httpsever.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include <string>
#include <iostream>
#include <thread>
HttpClient *HttpClient::instance = NULL;
template <typename Write> void Serialize_1(Write &w)
{
w.StartObject();
w.Key("devID");
w.String("xxx-xx-xx");
w.Key("serviceName");
w.String("vaserver");
w.Key("videoResource");
w.Int(10);
w.Key("serviceBaseUrl");
w.String("http://192.168.9.159:20083");
w.Key("proxy");
w.String("192.168.9.233");
w.EndObject();
}
void HttpClient::start()
{
std::thread th = std::thread(std::bind(&HttpClient::PostHeart, this));
th.detach