STTNet
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 宏定义 
Public 成员函数 | Public 属性 | 所有成员列表
stt::network::HttpClient类 参考

Http/Https客户端操作类 更多...

#include <sttnet.h>

类 stt::network::HttpClient 继承关系图:
stt::network::TcpClient

Public 成员函数

 HttpClient (const bool &TLS=false, const char *ca="", const char *cert="", const char *key="", const char *passwd="")
 HttpClient类的构造函数 更多...
 
bool getRequest (const std::string &url, const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=-1)
 发送一个GET请求到服务器 更多...
 
bool postRequest (const std::string &url, const std::string &body="", const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=-1)
 发送一个POST请求到服务器 更多...
 
bool getRequestFromFD (const int &fd, SSL *ssl, const std::string &url, const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=2)
 从tcp套接字发送一个GET请求到服务器 更多...
 
bool postRequestFromFD (const int &fd, SSL *ssl, const std::string &url, const std::string &body="", const std::string &header="", const std::string &header1="Connection: keep-alive", const int &sec=2)
 发送一个POST请求到服务器 更多...
 
bool isReturn ()
 获取服务器返回响应状态 更多...
 

Public 属性

std::string header =""
 服务器返回响应头 更多...
 
std::string body =""
 服务器返回响应体 更多...
 

详细描述

Http/Https客户端操作类

注解
-请求头都会自动带上Connection: keep-alive -如果需要重新设置TLS/Https加密的证书,目前需要销毁对象后重新构造 -如果没用传入套接字的函数,底层TCP默认是阻塞的

构造及析构函数说明

stt::network::HttpClient::HttpClient ( const bool &  TLS = false,
const char *  ca = "",
const char *  cert = "",
const char *  key = "",
const char *  passwd = "" 
)
inline

HttpClient类的构造函数

参数
TLStrue:启用Https加密 false:不启用Https加密 (默认为false不启用)
caCA 根证书路径(若启用TLS加密则必须填这个 默认空)
cert客户端证书路径(可选 默认空)
key客户端私钥路径(可选 默认空)
passwd私钥解密密码(可选 默认空)
注解
-ca用来校验对方服务器的证书是否可信(可以用操作系统自带的根证书验证) -如果启用了Https加密 ca必填 其他可选 -如果服务端要求客户端身份认证(双向 TLS/SSL),你需要提供一个有效的客户端证书。

成员函数说明

bool stt::network::HttpClient::getRequest ( const std::string &  url,
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = -1 
)

发送一个GET请求到服务器

注解
返回结果请调用isReturn函数判断;如果有返回结果,返回头和返回体存在header和body这两个全局变量中。
如果使用了TLS,会自动采用https协议,否则是自动采用http协议
默认是阻塞的
参数
urlhttp/https的完整url(注意需要显式指定端口和路径) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
headerHttp请求头;如果不是用createHeader生成,记得在末尾要加上
header1HTTP请求头的附加项;如果需要,一定要填入一个有效项;末尾不需要加入
(不能用createHeader)。(默认填入了keepalive项)
sec阻塞超时时间(s) 阻塞超过这个时间就不会再阻塞了 默认为-1 即无限等待
返回
true:请求发送成功 false:请求发送失败 注意:这不代表是否返回,只说明了发送成功
警告
需要http/https协议的完整url(注意需要显式指定端口和路径(就算路径不需要也要填入/)) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
bool stt::network::HttpClient::getRequestFromFD ( const int &  fd,
SSL *  ssl,
const std::string &  url,
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = 2 
)

从tcp套接字发送一个GET请求到服务器

注解
返回结果请调用isReturn函数判断;如果有返回结果,返回头和返回体存在header和body这两个全局变量中。
如果填入的ssl不为nullptr,会自动采用https协议,否则是自动采用http协议
调用的时候会阻塞,改变原有fd的状态,注意备份和恢复
参数
fdtcp套接字
sslTLS加密套接字
urlhttp/https的完整url(注意需要显式指定端口和路径) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
headerHttp请求头;如果不是用createHeader生成,记得在末尾要加上
header1HTTP请求头的附加项;如果需要,一定要填入一个有效项;末尾不需要加入
(不能用createHeader)。(默认填入了keepalive项)
sec阻塞超时时间(s) 阻塞超过这个时间就不会再阻塞了 默认为2s
返回
true:请求发送成功 false:请求发送失败 注意:这不代表是否返回,只说明了发送成功
警告
需要http/https协议的完整url(注意需要显式指定端口和路径(就算路径不需要也要填入/)) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
bool stt::network::HttpClient::isReturn ( )
inline

获取服务器返回响应状态

返回
true:服务器返回响应成功 false:服务器返回响应失败
bool stt::network::HttpClient::postRequest ( const std::string &  url,
const std::string &  body = "",
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = -1 
)

发送一个POST请求到服务器

注解
返回结果请调用isReturn函数判断;如果有返回结果,返回头和返回体存在header和body这两个全局变量中。
如果使用了TLS,会自动采用https协议,否则是自动采用http协议
默认是阻塞的
参数
urlhttp/https的完整url(注意需要显式指定端口和路径) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
bodyhttp请求体
headerHttp请求头;如果不是用createHeader生成,记得在末尾要加上
header1HTTP请求头的附加项;如果需要,一定要填入一个有效项;末尾不需要加入
(不能用createHeader)。(默认填入了keepalive项)
sec阻塞超时时间(s) 阻塞超过这个时间就不会再阻塞了 默认为-1 即无限等待
返回
true:请求发送成功 false:请求发送失败 注意:这不代表是否返回,只说明了发送成功
警告
需要http/https协议的完整url(注意需要显式指定端口和路径(就算路径不需要也要填入/)) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
bool stt::network::HttpClient::postRequestFromFD ( const int &  fd,
SSL *  ssl,
const std::string &  url,
const std::string &  body = "",
const std::string &  header = "",
const std::string &  header1 = "Connection: keep-alive",
const int &  sec = 2 
)

发送一个POST请求到服务器

注解
返回结果请调用isReturn函数判断;如果有返回结果,返回头和返回体存在header和body这两个全局变量中。
如果填入的ssl不为nullptr,会自动采用https协议,否则是自动采用http协议
调用的时候会阻塞,改变原有fd的状态,注意备份和恢复
参数
fdtcp套接字
sslTLS加密套接字
urlhttp/https的完整url(注意需要显式指定端口和路径) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)
bodyhttp请求体
headerHttp请求头;如果不是用createHeader生成,记得在末尾要加上
header1HTTP请求头的附加项;如果需要,一定要填入一个有效项;末尾不需要加入
(不能用createHeader)。(默认填入了keepalive项)
sec阻塞超时时间(s) 阻塞超过这个时间就不会再阻塞了 默认为-1 即无限等待
返回
true:请求发送成功 false:请求发送失败 注意:这不代表是否返回,只说明了发送成功
警告
需要http/https协议的完整url(注意需要显式指定端口和路径(就算路径不需要也要填入/)) 如:https://google.com 要写成https://google.com:443/ (补全:443和/)

类成员变量说明

std::string stt::network::HttpClient::body =""

服务器返回响应体

std::string stt::network::HttpClient::header =""

服务器返回响应头


该类的文档由以下文件生成: