1 Star 0 Fork 262

zlzong / libhv

forked from libhv / libhv 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
HttpClient.md 2.85 KB
一键复制 编辑 原始数据 按行查看 历史
ithewei 提交于 2023-09-26 21:49 . Update docs

HTTP 客户端类


class HttpClient {

    // 设置超时
    int setTimeout(int timeout);

    // 设置SSL/TLS
    int setSslCtx(hssl_ctx_t ssl_ctx);
    // 新建SSL/TLS
    int newSslCtx(hssl_ctx_opt_t* opt);

    // 清除全部请求头部
    int clearHeaders();
    // 设置请求头部
    int setHeader(const char* key, const char* value);
    // 删除请求头部
    int delHeader(const char* key);
    // 获取请求头部
    const char* getHeader(const char* key);

    // 设置http代理
    int setHttpProxy(const char* host, int port);
    // 设置https代理
    int setHttpsProxy(const char* host, int port);
    // 添加不走代理
    int addNoProxy(const char* host);

    // 同步发送
    int send(HttpRequest* req, HttpResponse* resp);
    // 异步发送
    int sendAsync(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);

    // 关闭连接 (HttpClient对象析构时会自动调用)
    int close();

};

namespace requests {

    // 同步请求
    Response request(Request req);
    Response request(http_method method, const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);

    // 上传文件
    Response uploadFile(const char* url, const char* filepath, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders);

    // 通过 `multipart/form-data` 格式上传文件
    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);

    // 上传大文件(带上传进度回调)
    Response uploadLargeFile(const char* url, const char* filepath, upload_progress_cb progress_cb = NULL, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders);

    // 下载文件 (更详细的断点续传示例代码见`examples/wget.cpp`)
    size_t downloadFile(const char* url, const char* filepath, download_progress_cb progress_cb = NULL);

    // HEAD 请求
    Response head(const char* url, const http_headers& headers = DefaultHeaders);

    // GET 请求
    Response get(const char* url, const http_headers& headers = DefaultHeaders);

    // POST 请求
    Response post(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);

    // PUT 请求
    Response put(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);

    // PATCH 请求
    Response patch(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders);

    // DELETE 请求
    Response Delete(const char* url, const http_headers& headers = DefaultHeaders);

    // 异步请求
    int async(Request req, ResponseCallback resp_cb);

}

测试代码见 examples/http_client_test.cpp

C++
1
https://gitee.com/zlzong/libhv.git
git@gitee.com:zlzong/libhv.git
zlzong
libhv
libhv
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891