Debian Codegraph @

third-party/libhv-v1.3.3/docs/cn/HttpClient.md

raw
1 HTTP 客户端类
2
3 ```c++
4
5 class HttpClient {
6
7 // 设置超时
8 int setTimeout(int timeout);
9
10 // 设置SSL/TLS
11 int setSslCtx(hssl_ctx_t ssl_ctx);
12 // 新建SSL/TLS
13 int newSslCtx(hssl_ctx_opt_t* opt);
14
15 // 清除全部请求头部
16 int clearHeaders();
17 // 设置请求头部
18 int setHeader(const char* key, const char* value);
19 // 删除请求头部
20 int delHeader(const char* key);
21 // 获取请求头部
22 const char* getHeader(const char* key);
23
24 // 设置http代理
25 int setHttpProxy(const char* host, int port);
26 // 设置https代理
27 int setHttpsProxy(const char* host, int port);
28 // 添加不走代理
29 int addNoProxy(const char* host);
30
31 // 同步发送
32 int send(HttpRequest* req, HttpResponse* resp);
33 // 异步发送
34 int sendAsync(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
35
36 // 关闭连接 (HttpClient对象析构时会自动调用)
37 int close();
38
39 };
40
41 namespace requests {
42
43 // 同步请求
44 Response request(Request req);
45 Response request(http_method method, const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);
46
47 // 上传文件
48 Response uploadFile(const char* url, const char* filepath, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders);
49
50 // 通过 `multipart/form-data` 格式上传文件
51 Response uploadFormFile(const char* url, const char* name, const char* filepath, std::map<std::string, std::string>& params = hv::empty_map, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders);
52
53 // 上传大文件(带上传进度回调)
54 Response uploadLargeFile(const char* url, const char* filepath, upload_progress_cb progress_cb = NULL, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders);
55
56 // 下载文件 (更详细的断点续传示例代码见`examples/wget.cpp`)
57 size_t downloadFile(const char* url, const char* filepath, download_progress_cb progress_cb = NULL);
58
59 // HEAD 请求
60 Response head(const char* url, const http_headers& headers = DefaultHeaders);
61
62 // GET 请求
63 Response get(const char* url, const http_headers& headers = DefaultHeaders);
64
65 // POST 请求
66 Response post(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);
67
68 // PUT 请求
69 Response put(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);
70
71 // PATCH 请求
72 Response patch(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);
73
74 // DELETE 请求
75 Response Delete(const char* url, const http_headers& headers = DefaultHeaders);
76
77 // 异步请求
78 int async(Request req, ResponseCallback resp_cb);
79
80 }
81
82 ```
83
84 测试代码见 [examples/http_client_test.cpp](../../examples/http_client_test.cpp)