NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_client类 参考

HTTP客户端类 更多...

#include <http_client.hpp>

struct  config
 HTTP客户端配置 更多...

Public 类型

using progress_callback_t = function<void(size_t, size_t)>
 进度回调类型
using error_callback_t = function<void(const exception&)>
 错误回调类型
using time_point = steady_clock::time_point
 时间点类型
using client_type = ssl_client
 底层客户端类型

Public 成员函数

 http_client ()
 默认构造函数
 http_client (config config)
 构造函数
 http_client (ssl_context ctx, config config)
 构造函数(带SSL上下文)
void set_config (config cfg)
 设置客户端配置
const configget_config () const noexcept
 获取客户端配置
const client_typeget_client () const noexcept
 获取底层客户端
void set_default_header (const string &key, string value)
 设置默认请求头
void remove_default_header (const string &key)
 移除默认请求头
void set_max_redirects (uint16_t max)
 设置最大重定向次数
void set_follow_redirects (bool follow)
 设置是否跟随重定向
void set_timeout (milliseconds timeout)
 设置统一超时时间
void set_proxy (string host, const ports port)
 设置代理
void clear_proxy ()
 清除代理设置
void set_progress_callback (progress_callback_t callback)
 设置进度回调
void set_error_callback (error_callback_t callback)
 设置错误回调
void set_ssl_context (ssl_context ctx)
 设置SSL上下文
void set_verify_ssl (bool verify)
 设置是否验证SSL证书
void clear_cookies ()
 清空Cookie存储
void set_cookie (const http_cookie &c, const string &domain, const string &path="/")
 设置Cookie
NEFORCE_NODISCARD unordered_map< string, http_cookieget_cookies () const
 获取所有Cookie
http_client_response get (const string &url, const unordered_map< string, string > &headers={})
 发送GET请求
http_client_response post (const string &url, const string &body="", const string &content_type="application/x-www-form-urlencoded", const unordered_map< string, string > &headers={})
 发送POST请求
http_client_response post_json (const string &url_str, const string &json_body, const unordered_map< string, string > &headers)
 发送POST JSON请求
http_client_response post_form (const string &url_str, const unordered_map< string, string > &form_data, const unordered_map< string, string > &headers)
 发送POST表单请求
http_client_response put (const string &url, const string &body="", const string &content_type="application/x-www-form-urlencoded", const unordered_map< string, string > &headers={})
 发送PUT请求
http_client_response del (const string &url, const unordered_map< string, string > &headers={})
 发送DELETE请求
http_client_response head (const string &url, const unordered_map< string, string > &headers={})
 发送HEAD请求
http_client_response options (const string &url, const unordered_map< string, string > &headers={})
 发送OPTIONS请求
http_client_response patch (const string &url, const string &body="", const string &content_type="application/x-www-form-urlencoded", const unordered_map< string, string > &headers={})
 发送PATCH请求
http_client_response request (http_client_request req)
 发送自定义HTTP请求
bool download_file (const string &url, path output, bool is_binary=true)
 下载文件
future< http_client_responserequest_async (http_client_request req)
 异步HTTP请求
void close ()
 关闭连接
NEFORCE_NODISCARD bool is_connected () const noexcept
 检查是否已连接

详细描述

HTTP客户端类

提供完整的HTTP客户端功能,支持同步和异步请求。

主要功能:

  • HTTP/HTTPS请求
  • 自动重定向处理
  • Cookie持久化
  • 请求/响应超时控制
  • 代理支持
  • 文件下载
  • 异步请求
  • SSL/TLS支持
  • 进度回调
  • 分块传输处理

使用示例:

// 创建HTTP客户端
http_client client;
// GET请求
auto response = client.get("https://api.example.com/users");
if (response.is_success()) {
println(response.body);
}
// POST JSON请求
string json = R"({"name": "John", "age": 30})";
auto post_resp = client.post_json("https://api.example.com/users", json);
// POST表单请求
unordered_map<string, string> form = {{"username", "john"}, {"password", "123"}};
auto form_resp = client.post_form("https://example.com/login", form);
// 设置自定义请求头
unordered_map<string, string> headers = {{"Authorization", "Bearer token123"}};
auto auth_resp = client.get("https://api.example.com/profile", headers);
// 下载文件
client.download_file("https://example.com/file.zip", "/path/to/save/file.zip");
// 设置代理
client.set_proxy("proxy.example.com", 8080);
// 设置超时
client.set_timeout(milliseconds(5000));
// 异步请求
auto future = client.request_async(req);
auto async_resp = future.get();
bool download_file(const string &url, path output, bool is_binary=true)
下载文件
future< http_client_response > request_async(http_client_request req)
异步HTTP请求
http_client_response post_form(const string &url_str, const unordered_map< string, string > &form_data, const unordered_map< string, string > &headers)
发送POST表单请求
http_client_response get(const string &url, const unordered_map< string, string > &headers={})
发送GET请求
void set_proxy(string host, const ports port)
设置代理
http_client_response post_json(const string &url_str, const string &json_body, const unordered_map< string, string > &headers)
发送POST JSON请求
http_client()
默认构造函数
void set_timeout(milliseconds timeout)
设置统一超时时间
无序映射容器
void println(Args &&... args)
打印多个值并换行
duration< int64_t, milli > milliseconds
毫秒持续时间

在文件 http_client.hpp80 行定义.

构造及析构函数说明

◆ http_client() [1/2]

http_client::http_client ( config config)
explicit

构造函数

参数
config客户端配置

◆ http_client() [2/2]

http_client::http_client ( ssl_context ctx,
config config )
explicit

构造函数(带SSL上下文)

参数
ctxSSL上下文
config客户端配置

引用了 http_client().

成员函数说明

◆ del()

http_client_response http_client::del ( const string & url,
const unordered_map< string, string > & headers = {} )

发送DELETE请求

参数
url请求URL
headers自定义请求头
返回
HTTP响应对象

发送HTTP DELETE请求到指定URL。 用于删除指定资源。

◆ download_file()

bool http_client::download_file ( const string & url,
path output,
bool is_binary = true )

下载文件

参数
url文件URL
output输出路径
is_binary是否为二进制文件
返回
下载成功返回true

◆ get()

http_client_response http_client::get ( const string & url,
const unordered_map< string, string > & headers = {} )

发送GET请求

参数
url请求URL
headers自定义请求头
返回
HTTP响应对象

发送HTTP GET请求到指定URL。 支持HTTP和HTTPS协议。 自动处理Cookie和重定向。

◆ get_client()

const client_type & http_client::get_client ( ) const
inlinenoexcept

获取底层客户端

返回
客户端引用

在文件 http_client.hpp178 行定义.

◆ get_config()

const config & http_client::get_config ( ) const
inlinenoexcept

获取客户端配置

返回
配置常量引用

在文件 http_client.hpp172 行定义.

◆ get_cookies()

NEFORCE_NODISCARD unordered_map< string, http_cookie > http_client::get_cookies ( ) const
inline

获取所有Cookie

返回
Cookie映射

在文件 http_client.hpp284 行定义.

◆ head()

http_client_response http_client::head ( const string & url,
const unordered_map< string, string > & headers = {} )

发送HEAD请求

参数
url请求URL
headers自定义请求头
返回
HTTP响应对象

发送HTTP HEAD请求到指定URL。 只获取响应头,不获取响应正文。 适用于检查资源是否存在或获取元信息。

◆ is_connected()

NEFORCE_NODISCARD bool http_client::is_connected ( ) const
inlinenoexcept

检查是否已连接

返回
已连接返回true

在文件 http_client.hpp445 行定义.

◆ options()

http_client_response http_client::options ( const string & url,
const unordered_map< string, string > & headers = {} )

发送OPTIONS请求

参数
url请求URL
headers自定义请求头
返回
HTTP响应对象

发送HTTP OPTIONS请求到指定URL。 用于获取服务器支持的HTTP方法列表。 常用于CORS预检请求。

◆ patch()

http_client_response http_client::patch ( const string & url,
const string & body = "",
const string & content_type = "application/x-www-form-urlencoded",
const unordered_map< string, string > & headers = {} )

发送PATCH请求

参数
url请求URL
body请求正文
content_typeContent-Type头值
headers自定义请求头
返回
HTTP响应对象

发送HTTP PATCH请求到指定URL。 用于部分更新资源,只发送需要修改的字段。 默认Content-Type为application/x-www-form-urlencoded。

◆ post()

http_client_response http_client::post ( const string & url,
const string & body = "",
const string & content_type = "application/x-www-form-urlencoded",
const unordered_map< string, string > & headers = {} )

发送POST请求

参数
url请求URL
body请求正文
content_typeContent-Type头值
headers自定义请求头
返回
HTTP响应对象

发送HTTP POST请求到指定URL。 默认Content-Type为application/x-www-form-urlencoded。 支持HTTP和HTTPS协议。 自动处理Cookie和重定向。

◆ post_form()

http_client_response http_client::post_form ( const string & url_str,
const unordered_map< string, string > & form_data,
const unordered_map< string, string > & headers )

发送POST表单请求

参数
url_str请求URL
form_data表单数据键值对
headers自定义请求头
返回
HTTP响应对象

发送application/x-www-form-urlencoded格式的POST请求。 自动将表单数据编码为URL编码格式。 适用于HTML表单提交场景。

◆ post_json()

http_client_response http_client::post_json ( const string & url_str,
const string & json_body,
const unordered_map< string, string > & headers )

发送POST JSON请求

参数
url_str请求URL
json_bodyJSON格式的请求正文
headers自定义请求头
返回
HTTP响应对象

发送JSON格式的POST请求。 Content-Type自动设置为application/json。 适用于RESTful API调用。

◆ put()

http_client_response http_client::put ( const string & url,
const string & body = "",
const string & content_type = "application/x-www-form-urlencoded",
const unordered_map< string, string > & headers = {} )

发送PUT请求

参数
url请求URL
body请求正文
content_typeContent-Type头值
headers自定义请求头
返回
HTTP响应对象

发送HTTP PUT请求到指定URL。 用于更新资源,通常包含完整的资源表示。 默认Content-Type为application/x-www-form-urlencoded。

◆ remove_default_header()

void http_client::remove_default_header ( const string & key)
inline

移除默认请求头

参数
key头名称

在文件 http_client.hpp195 行定义.

◆ request()

http_client_response http_client::request ( http_client_request req)

发送自定义HTTP请求

参数
req请求对象
返回
响应对象

◆ request_async()

future< http_client_response > http_client::request_async ( http_client_request req)

异步HTTP请求

参数
req请求对象
返回
future响应对象

◆ set_config()

void http_client::set_config ( config cfg)
inline

设置客户端配置

参数
cfg新配置

在文件 http_client.hpp163 行定义.

引用了 move() , 以及 set_config().

被这些函数引用 set_config().

◆ set_cookie()

void http_client::set_cookie ( const http_cookie & c,
const string & domain,
const string & path = "/" )

设置Cookie

参数
cCookie对象
domain域名
path路径

◆ set_default_header()

void http_client::set_default_header ( const string & key,
string value )
inline

设置默认请求头

参数
key头名称
value头值

在文件 http_client.hpp186 行定义.

引用了 move().

◆ set_error_callback()

void http_client::set_error_callback ( error_callback_t callback)
inline

设置错误回调

参数
callback回调函数

在文件 http_client.hpp250 行定义.

引用了 move().

◆ set_follow_redirects()

void http_client::set_follow_redirects ( bool follow)
inline

设置是否跟随重定向

参数
follow是否跟随

在文件 http_client.hpp210 行定义.

◆ set_max_redirects()

void http_client::set_max_redirects ( uint16_t max)
inline

设置最大重定向次数

参数
max最大次数

在文件 http_client.hpp204 行定义.

引用了 max().

◆ set_progress_callback()

void http_client::set_progress_callback ( progress_callback_t callback)
inline

设置进度回调

参数
callback回调函数

在文件 http_client.hpp244 行定义.

引用了 move().

◆ set_proxy()

void http_client::set_proxy ( string host,
const ports port )
inline

设置代理

参数
host代理主机
port代理端口

在文件 http_client.hpp227 行定义.

引用了 move().

◆ set_ssl_context()

void http_client::set_ssl_context ( ssl_context ctx)

设置SSL上下文

参数
ctxSSL上下文

◆ set_timeout()

void http_client::set_timeout ( milliseconds timeout)
inline

设置统一超时时间

参数
timeout超时时间

在文件 http_client.hpp216 行定义.

引用了 timeout.

◆ set_verify_ssl()

void http_client::set_verify_ssl ( bool verify)

设置是否验证SSL证书

参数
verify是否验证

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