1
0

http_client.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef HV_HTTP_CLIENT_H_
  2. #define HV_HTTP_CLIENT_H_
  3. #include "hexport.h"
  4. #include "HttpMessage.h"
  5. /*
  6. #include <stdio.h>
  7. #include "http_client.h"
  8. int main(int argc, char* argv[]) {
  9. HttpRequest req;
  10. req.method = HTTP_GET;
  11. req.url = "http://www.example.com";
  12. HttpResponse res;
  13. int ret = http_client_send(&req, &res);
  14. printf("%s\n", req.Dump(true,true).c_str());
  15. if (ret != 0) {
  16. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  17. }
  18. else {
  19. printf("%s\n", res.Dump(true,true).c_str());
  20. }
  21. return ret;
  22. }
  23. */
  24. #define DEFAULT_HTTP_TIMEOUT 30 // s
  25. typedef struct http_client_s http_client_t;
  26. HV_EXPORT http_client_t* http_client_new(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0);
  27. HV_EXPORT int http_client_del(http_client_t* cli);
  28. HV_EXPORT const char* http_client_strerror(int errcode);
  29. HV_EXPORT int http_client_set_timeout(http_client_t* cli, int timeout);
  30. // common headers
  31. HV_EXPORT int http_client_clear_headers(http_client_t* cli);
  32. HV_EXPORT int http_client_set_header(http_client_t* cli, const char* key, const char* value);
  33. HV_EXPORT int http_client_del_header(http_client_t* cli, const char* key);
  34. HV_EXPORT const char* http_client_get_header(http_client_t* cli, const char* key);
  35. // sync
  36. HV_EXPORT int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp);
  37. // async
  38. // Intern will start an EventLoopThread when http_client_send_async first called,
  39. // http_client_del will destroy the thread.
  40. HV_EXPORT int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  41. // top-level api
  42. // http_client_new -> http_client_send -> http_client_del
  43. HV_EXPORT int http_client_send(HttpRequest* req, HttpResponse* resp);
  44. // http_client_send_async(&default_async_client, ...)
  45. HV_EXPORT int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  46. #endif // HV_HTTP_CLIENT_H_