1
0

http_client_test.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "requests.h"
  2. #include "hthread.h" // import hv_gettid
  3. static void test_http_async_client(http_client_t* cli, int* finished) {
  4. printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
  5. HttpRequestPtr req(new HttpRequest);
  6. req->method = HTTP_POST;
  7. req->url = "127.0.0.1:8080/echo";
  8. req->headers["Connection"] = "keep-alive";
  9. req->body = "This is an async request.";
  10. req->timeout = 10;
  11. http_client_send_async(cli, req, [finished](const HttpResponsePtr& resp) {
  12. printf("test_http_async_client response thread tid=%ld\n", hv_gettid());
  13. if (resp == NULL) {
  14. printf("request failed!\n");
  15. } else {
  16. printf("%d %s\r\n", resp->status_code, resp->status_message());
  17. printf("%s\n", resp->body.c_str());
  18. }
  19. *finished = 1;
  20. });
  21. }
  22. static void test_http_sync_client(http_client_t* cli) {
  23. HttpRequest req;
  24. req.method = HTTP_POST;
  25. req.url = "127.0.0.1:8080/echo";
  26. req.headers["Connection"] = "keep-alive";
  27. req.body = "This is a sync request.";
  28. req.timeout = 10;
  29. HttpResponse resp;
  30. int ret = http_client_send(cli, &req, &resp);
  31. if (ret != 0) {
  32. printf("request failed!\n");
  33. } else {
  34. printf("%d %s\r\n", resp.status_code, resp.status_message());
  35. printf("%s\n", resp.body.c_str());
  36. }
  37. }
  38. static void test_requests() {
  39. // auto resp = requests::get("http://www.example.com");
  40. //
  41. // make clean && make WITH_OPENSSL=yes
  42. // auto resp = requests::get("https://www.baidu.com");
  43. auto resp = requests::get("http://127.0.0.1:8080/ping");
  44. if (resp == NULL) {
  45. printf("request failed!\n");
  46. } else {
  47. printf("%d %s\r\n", resp->status_code, resp->status_message());
  48. printf("%s\n", resp->body.c_str());
  49. }
  50. hv::Json jroot;
  51. jroot["user"] = "admin";
  52. jroot["pswd"] = "123456";
  53. http_headers headers;
  54. headers["Content-Type"] = "application/json";
  55. resp = requests::post("127.0.0.1:8080/echo", jroot.dump(), headers);
  56. if (resp == NULL) {
  57. printf("request failed!\n");
  58. } else {
  59. printf("%d %s\r\n", resp->status_code, resp->status_message());
  60. printf("%s\n", resp->body.c_str());
  61. }
  62. }
  63. int main(int argc, char* argv[]) {
  64. int cnt = 0;
  65. if (argc > 1) cnt = atoi(argv[1]);
  66. if (cnt == 0) cnt = 1;
  67. http_client_t* sync_client = http_client_new();
  68. http_client_t* async_client = http_client_new();
  69. int finished = 0;
  70. for (int i = 0; i < cnt; ++i) {
  71. test_http_async_client(async_client, &finished);
  72. test_http_sync_client(sync_client);
  73. // like python requests
  74. test_requests();
  75. }
  76. http_client_del(sync_client);
  77. // demo wait async finished
  78. while (!finished) hv_delay(100);
  79. printf("finished!\n");
  80. http_client_del(async_client);
  81. return 0;
  82. }