Sion
- Sion是一个轻量级的c++ http客户端,仅单头文件450行,自带std::string的扩展
- Sion is a lightweight C + + HTTP Client, with only one header file 450 lines, with its own std::string extension.
- Sion由Myhttp(暂未取名)删除服务器相关部分代码而来,仅能作为HttpClient使用。仅支持http协议,本来是打算加入https的,发现加入后根本不能单头文件就实现支持http,https的client。https,ws还是放在体积大点的myHttp吧。
例子
#include<iostream>
#include"Sion.h"
MyString PostTest()
{
Sion::Request request;
request.Header["Content-Type"] = "application/json; charset=utf-8";
request.RequestBody = R"({"id":null,"password":"zanllp_pw","account":"zanllp"})";
Sion::Response response = request.SendRequest(Sion::Post, "http://127.0.0.1:5000/api/auth");
response.ParseFromSource();
return response.ResponseBody;
}
MyString ChunkedTest()
{
Sion::Response response = Sion::Request::StaticRequest(Sion::Get, "http://zanllp.cn");
response.ParseFromSource(true);
return response.ResponseBody;
}
void ShowHeader()
{
Sion::Request request;
Sion::Response response=request.SendRequest(Sion::Get, "http://www.baidu.com");
response.ParseFromSource(true);
for (auto x : response.Header)
{
std::cout << x.first << " " << x.second << std::endl;
}
}
int main()
{
try
{
ShowHeader();
std::cout << ChunkedTest() <<std::endl;
std::cout << PostTest() << std::endl;
}
catch (const std::exception &e)
{
std::cout << e.what() << std::endl;
}
system("pause");
}
MyString
该类继承std::string,用法基本一致,拓展了几个函数
std::vector<MyString> Split(MyString flag, int num = 0, bool skipEmpty = true)
MyString Trim(char target = ' ')
bool HasLetter()
MyString ToGbk()
std::vector<int> FindAll(MyString flag, int num = -1)
MyString& Replace(MyString oldStr, MyString newStr)
Response
该类用来处理请求响应
MyString ParseFromSource(bool ConverToGbk = false)
Request
该类用来处理发送请求
void SetHttpMethod(MethodEnum method, MyString other = "")
MyString SendRequest(MyString url)
MyString SendRequest(MethodEnum method, MyString url)
MyString static StaticRequest(MethodEnum method, MyString url)
GitHub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)