16 Star 91 Fork 31

ithewei / libhv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
http_client_test.cpp 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
hewei.it 提交于 2021-02-07 11:01 . update
#include "requests.h"
#include "hthread.h" // import hv_gettid
static void test_http_async_client(int* finished) {
printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
HttpRequestPtr req(new HttpRequest);
req->method = HTTP_POST;
req->url = "127.0.0.1:8080/echo";
req->headers["Connection"] = "keep-alive";
req->body = "this is an async request.";
req->timeout = 10;
http_client_send_async(req, [finished](const HttpResponsePtr& resp) {
printf("test_http_async_client response thread tid=%ld\n", hv_gettid());
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
*finished = 1;
});
}
static void test_http_sync_client() {
// auto resp = requests::get("http://www.example.com");
//
// make clean && make WITH_OPENSSL=yes
// auto resp = requests::get("https://www.baidu.com");
auto resp = requests::get("http://127.0.0.1:8080/ping");
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
hv::Json jroot;
jroot["user"] = "admin";
http_headers headers;
headers["Content-Type"] = "application/json";
resp = requests::post("127.0.0.1:8080/echo", jroot.dump(), headers);
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%d %s\r\n", resp->status_code, resp->status_message());
printf("%s\n", resp->body.c_str());
}
}
int main() {
int finished = 0;
int cnt = 1;
for (int i = 0; i < cnt; ++i) {
test_http_async_client(&finished);
test_http_sync_client();
hv_delay(1000);
}
// demo wait async ResponseCallback
while (!finished) {
hv_delay(100);
}
printf("finished!\n");
return 0;
}
C
1
https://gitee.com/ithewei/libhv.git
git@gitee.com:ithewei/libhv.git
ithewei
libhv
libhv
master

搜索帮助