[项目]http+json

本文介绍了一个使用http+json架构的项目,选择了cpp-httplib和rapidjson这两个头文件only的开源库进行通信。通过POST方式发送请求,并展示了包含JSON序列化和反序列化的调试demo。该示例在Ubuntu 16.04环境下编译,依赖C++11和pthread库。
摘要由CSDN通过智能技术生成

目录

背景:项目需要采用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值