|
|
| http_client () |
| | 默认构造函数
|
| | http_client (config config) |
| | 构造函数
|
| | http_client (ssl_context ctx, config config) |
| | 构造函数(带SSL上下文)
|
| void | set_config (config cfg) |
| | 设置客户端配置
|
| const config & | get_config () const noexcept |
| | 获取客户端配置
|
| const client_type & | get_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_cookie > | get_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_response > | request_async (http_client_request req) |
| | 异步HTTP请求
|
|
void | close () |
| | 关闭连接
|
| NEFORCE_NODISCARD bool | is_connected () const noexcept |
| | 检查是否已连接
|
HTTP客户端类
提供完整的HTTP客户端功能,支持同步和异步请求。
主要功能:
- HTTP/HTTPS请求
- 自动重定向处理
- Cookie持久化
- 请求/响应超时控制
- 代理支持
- 文件下载
- 异步请求
- SSL/TLS支持
- 进度回调
- 分块传输处理
使用示例:
auto response = client.
get(
"https://api.example.com/users");
if (response.is_success()) {
}
string json = R"({"name": "John", "age": 30})";
auto post_resp = client.
post_json(
"https://api.example.com/users", json);
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");
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请求
void set_timeout(milliseconds timeout)
设置统一超时时间
void println(Args &&... args)
打印多个值并换行
duration< int64_t, milli > milliseconds
毫秒持续时间
在文件 http_client.hpp 第 80 行定义.