NHttpClient
- 接口
| 接口 | 返回值 | 说明 |
|---|---|---|
| SetHttpType | void | 设置http请求类型 |
| GetHttpType | EHttpTypes | 获取http请求类型 |
| SetHttpRequestHeader | void | 设置http请求报文头部信息 |
| GetHttpRequestHeader | std::unordered_map<std::string, std::string> | 获取http请求报文头部信息 |
| SetHttpRequestBody | void | 设置http请求报文body信息 |
| GetHttpRequestBody | std::string | 获取http请求报文body信息 |
| SetHttpRequestUri | void | 设置http请求报文uri |
| GetHttpRequestUri | std::string | 获取http请求报文uri |
| Start | HttpResponseMsg | http请求 |
| BuildRequestPacket | std::string | 构建http请求报文字符串 |
| SetServerType | void | 设置服务类型 |
| 结构体 |
cpp
struct HttpRequestMsg
{
EHttpTypes requestType;//请求类型
std::string requestBody;//请求报文的body内容
std::unordered_map<std::string, std::string> requestHeader;//请求报文的头部信息,Host为必须项
std::string requestUri;//请求报文的Uri
size_t receiveSize = 4096;//接收信息的最大长度
};
struct HttpResponseMsg
{
int responseCode;//响应码
std::unordered_map<std::string, std::string> responseHeader;//响应报文头部信息
std::string responseBody;//响应报文body内容
std::string version;//版本号
std::string status;//状态
};| 接口 | 参数 | 描述 |
|---|---|---|
| void SetHttpType(EHttpTypes type); EHttpTypes GetHttpType(); | type:http请求类型,如Get、Post | 设置/获取Http请求类型 |
| void SetHttpRequestHeader(std::unordered_map<std::string, std::string> requestHeader); std::unordered_map<std::string, std::string> GetHttpRequestHeader(); | requestHeader:请求报文头部信息,Host为必须项 | 设置/获取请求报文头部信息 |
| void SetHttpRequestBody(std::string& body); std::string GetHttpRequestBody(); | body:请求报文body内容 | 设置/获取请求报文body内容 |
| void SetHttpRequestUri(std::string& uri); std::string GetHttpRequestUri(); | uri:请求报文uri | 设置/获取uri |
| HttpResponseMsg Start(std::error_code& ec); | 进行Http请求 | |
| void Start(std::function<void(const HttpResponseMsg&, const std::error_code&)> callback); | callback:异步回调函数 | 进行http异步请求 |
| HttpResponseMsg Start(HttpRequestMsg request, std::error_code& ec); | request:自定义请求报文信息 | 进行Http请求 |
| std::string BuildRequestPacket(); | 构建请求报文 | |
| void SetServerType(std::string& value); | value:服务类型,“http”或“https” | 设置请求服务类型 |
描述:可通过自定义报文信息进行Http或Https请求 示例代码:
cpp
void Http::HttpsGet()
{
std::error_code ec;
NHttpClientPtr m_HttpClient = NNetWorkManager::CreateHttpClient();
m_HttpClient->SetHttpType(EHttpTypes::HT_GET);
std::string uri = "http://www.example.com";
m_HttpClient->SetHttpRequestUri(uri);
std::unordered_map<std::string, std::string> header1;
header1["Host"] = "www.example.com";
header1["Connection"] = "close";
m_HttpClient->SetHttpRequestHeader(header1);
std::string type = "https";
m_HttpClient->SetServerType(type);
HttpResponseMsg hr = m_HttpClient->Start(ec);
}cpp
void Http::HttpsGetAsync(NActorPtr actor)
{
std::error_code ec;
NHttpClientPtr m_HttpClient = NNetWorkManager::CreateHttpClient();
m_HttpClient->SetHttpType(EHttpTypes::HT_GET);
std::string uri = "http://www.baidu.com";
m_HttpClient->SetHttpRequestUri(uri);
std::unordered_map<std::string, std::string> header1;
header1["Host"] = "www.baidu.com";
header1["Connection"] = "close";
m_HttpClient->SetHttpRequestHeader(header1);
std::string type = "https";
m_HttpClient->SetServerType(type);
m_HttpClient->Start(std::bind(&Http::HttpHandle, this, std::placeholders::_1, std::placeholders::_2));
}
void Http::HttpHandle(const HttpResponseMsg& msg, const std::error_code& ec)
{
if (!ec)
{
LogEngine("Version: %s", msg.version.c_str());
LogEngine("Response Code: %d", msg.responseCode);
LogEngine("Status: %s", msg.status.c_str());
LogEngine("Header: ");
for (auto header : msg.responseHeader) {
LogEngine("%s : %s", header.first.c_str(), header.second.c_str());
}
if (msg.responseBody.length() < 500)
{
LogEngine("Body: %s", msg.responseBody.c_str());
}
else
{
LogEngine("The content of the request body is too long. If you want to view it, please enable log output in the code");
//LogEngine("Body: %s", msg.responseBody.c_str());
}
}
else
{
LogEngine("异步操作异常, 错误码:%s", ec.message().c_str());
}
}
