| 1 |
```c++ |
| 2 |
|
| 3 |
class HttpMessage { |
| 4 |
// 设置/获取头部 |
| 5 |
void SetHeader(const char* key, const std::string& value); |
| 6 |
std::string GetHeader(const char* key, const std::string& defvalue = hv::empty_string); |
| 7 |
|
| 8 |
// 添加/获取cookie |
| 9 |
void AddCookie(const HttpCookie& cookie); |
| 10 |
const HttpCookie& GetCookie(const std::string& name); |
| 11 |
|
| 12 |
// 设置/获取 `Content-Type` |
| 13 |
void SetContentType(http_content_type type); |
| 14 |
http_content_type ContentType(); |
| 15 |
|
| 16 |
// 获取 `Content-Length` |
| 17 |
size_t ContentLength(); |
| 18 |
|
| 19 |
// 填充数据 |
| 20 |
void SetBody(const std::string& body); |
| 21 |
// 获取数据 |
| 22 |
const std::string& Body(); |
| 23 |
// 解析数据 |
| 24 |
int ParseBody(); |
| 25 |
|
| 26 |
// 填充/获取 `application/json` 格式数据 |
| 27 |
template<typename T> |
| 28 |
int Json(const T& t); |
| 29 |
const hv::Json& GetJson(); |
| 30 |
|
| 31 |
// 填充/获取 `multipart/form-data` 格式数据 |
| 32 |
template<typename T> |
| 33 |
void SetFormData(const char* name, const T& t); |
| 34 |
void SetFormFile(const char* name, const char* filepath); |
| 35 |
std::string GetFormData(const char* name, const std::string& defvalue = hv::empty_string); |
| 36 |
int SaveFormFile(const char* name, const char* path); |
| 37 |
|
| 38 |
// 填充/获取 `application/x-www-urlencoded` 格式数据 |
| 39 |
template<typename T> |
| 40 |
void SetUrlEncoded(const char* key, const T& t); |
| 41 |
std::string GetUrlEncoded(const char* key, const std::string& defvalue = hv::empty_string); |
| 42 |
|
| 43 |
// 根据 `Content-Type` 填充对应格式数据 |
| 44 |
template<typename T> |
| 45 |
void Set(const char* key, const T& value); |
| 46 |
// 根据 `Content-Type` 获取对应格式数据 |
| 47 |
template<typename T> |
| 48 |
T Get(const char* key, T defvalue = 0); |
| 49 |
// 根据 `Content-Type` 获取对应格式数据并转换成字符串 |
| 50 |
std::string GetString(const char* key, const std::string& = ""); |
| 51 |
// 根据 `Content-Type` 获取对应格式数据并转换成Boolean类型 |
| 52 |
bool GetBool(const char* key, bool defvalue = 0); |
| 53 |
// 根据 `Content-Type` 获取对应格式数据并转换成整型 |
| 54 |
int64_t GetInt(const char* key, int64_t defvalue = 0); |
| 55 |
// 根据 `Content-Type` 获取对应格式数据并转换成浮点数 |
| 56 |
double GetFloat(const char* key, double defvalue = 0); |
| 57 |
}; |
| 58 |
|
| 59 |
// HttpRequest 继承自 HttpMessage |
| 60 |
class HttpRequest : public HttpMessage { |
| 61 |
// 设置/获取method |
| 62 |
void SetMethod(const char* method); |
| 63 |
const char* Method(); |
| 64 |
|
| 65 |
// 设置URL |
| 66 |
void SetUrl(const char* url); |
| 67 |
// 获取URL |
| 68 |
const std::string& Url(); |
| 69 |
// 解析URL |
| 70 |
void ParseUrl(); |
| 71 |
// 获取Host |
| 72 |
std::string Host(); |
| 73 |
// 获取Path |
| 74 |
std::string Path(); |
| 75 |
|
| 76 |
// 设置/获取参数 |
| 77 |
template<typename T> |
| 78 |
void SetParam(const char* key, const T& t); |
| 79 |
std::string GetParam(const char* key, const std::string& defvalue = hv::empty_string); |
| 80 |
|
| 81 |
// 设置代理 |
| 82 |
void SetProxy(const char* host, int port); |
| 83 |
|
| 84 |
// 设置认证 |
| 85 |
void SetAuth(const std::string& auth); |
| 86 |
void SetBasicAuth(const std::string& username, const std::string& password); |
| 87 |
void SetBearerTokenAuth(const std::string& token); |
| 88 |
|
| 89 |
// 设置请求超时 |
| 90 |
void SetTimeout(int sec); |
| 91 |
// 设置连接超时 |
| 92 |
void SetConnectTimeout(int sec); |
| 93 |
// 允许重定向 |
| 94 |
void AllowRedirect(bool on = true); |
| 95 |
// 设置重试 |
| 96 |
void SetRetry(int count = DEFAULT_HTTP_FAIL_RETRY_COUNT, |
| 97 |
int delay = DEFAULT_HTTP_FAIL_RETRY_DELAY); |
| 98 |
// 取消 |
| 99 |
void Cancel(); |
| 100 |
}; |
| 101 |
|
| 102 |
// HttpResponse 继承自 HttpMessage |
| 103 |
class HttpResponse : public HttpMessage { |
| 104 |
// 状态码 |
| 105 |
http_status status_code; |
| 106 |
// 状态字符串 |
| 107 |
const char* status_message(); |
| 108 |
}; |
| 109 |
|
| 110 |
``` |