1 Star 0 Fork 270

HuBin95 / libhv

forked from libhv / libhv 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
HttpClient.md 2.62 KB
一键复制 编辑 原始数据 按行查看 历史
ithewei 提交于 2023-03-08 21:15 . 添加c++接口文档

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);

    // 下载文件 (更详细的断点续传示例代码见`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

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/hubin95/libhv.git
git@gitee.com:hubin95/libhv.git
hubin95
libhv
libhv
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891