http_client_plus.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_CLIENT
  4. #include <functional>
  5. #include "http_header.h"
  6. #include "http_client_cache.h"
  7. #include "http_cookie.h"
  8. #include "make_form.h"
  9. class http_client_listener;
  10. namespace ylib
  11. {
  12. namespace network
  13. {
  14. namespace http
  15. {
  16. class client_plus :public ylib::error_base
  17. {
  18. public:
  19. client_plus();
  20. ~client_plus();
  21. void close();
  22. void set_timeout(uint32 connect_msec = 3000, uint32 recv_msec = 8000);
  23. bool del(const std::string& url, const std::map<std::string, std::string>& value = std::map<std::string, std::string>());
  24. bool get(const std::string& url, const std::map<std::string, std::string>& value = std::map<std::string, std::string>(), bool wait = true);
  25. bool post(const std::string& url, const std::map<std::string, std::string>& value, bool to_utf8 = false);
  26. bool post(const std::string& url, const ylib::json& value, bool to_utf8 = false);
  27. bool post(const std::string& url, const ylib::buffer& value);
  28. bool post(const std::string& url, const http::make_form& value);
  29. bool head(const std::string& url);
  30. network::http::header_list& headers_request();
  31. network::http::header_list& headers_response();
  32. uint32 status();
  33. ylib::buffer& response();
  34. /*设置缓存*/
  35. void cache(client_cache* cache);
  36. /*请求路径*/
  37. inline const std::string& url() { return m_url; }
  38. /*cookie*/
  39. inline network::http::cookie& cookie() { return m_cookie; }
  40. inline void cookie(const network::http::cookie& ck) { m_cookie = ck; }
  41. /*[回调] 正在下载*/
  42. void on_down_ing(const std::function<bool(void* data, uint32 downsize, uint64 alldownsize, uint64 allsize, network::http::client_plus& client)>& callback);
  43. /*[回调] 下载结束*/
  44. void on_down_end(const std::function<void(network::http::client_plus& client)>& callback);
  45. /*[回调] 下载失败*/
  46. void on_down_failed(const std::function<void(network::http::client_plus& client)>& callback);
  47. // 清理所有Cookie缓存
  48. static void clear_all_cookies();
  49. friend class http_client_listener;
  50. public:
  51. uint64 m_temp[10];
  52. private:
  53. bool parseurl(const std::string& url);
  54. bool connect();
  55. bool init();
  56. bool request();
  57. bool post(const std::string& url);
  58. void* client();
  59. private:
  60. // HP客户端
  61. void* m_client;
  62. // HP客户端-SSL
  63. void* m_client_ssl;
  64. // HP监听器
  65. http_client_listener* m_listener;
  66. // 是否已初始化
  67. bool m_init;
  68. // 连接IP
  69. std::string m_ipaddress;
  70. // 连接端口
  71. ushort m_port;
  72. // HTTPS
  73. bool m_ssl;
  74. // 请求链接
  75. std::string m_url;
  76. // 请求路径
  77. std::string m_path;
  78. // 请求方式
  79. network::http::method m_method;
  80. // 请求数据
  81. ylib::buffer m_request_body;
  82. // [header] 请求
  83. network::http::header_list m_headers_request;
  84. // 超时时间 连接
  85. timestamp m_timeout_connect_msec;
  86. // 超时时间 接收
  87. timestamp m_timeout_recv_msec;
  88. // 缓存
  89. client_cache* m_cache;
  90. // cookie
  91. network::http::cookie m_cookie;
  92. // 请求等待
  93. bool m_request_wait = true;
  94. #ifndef _WIN32
  95. public:
  96. #endif
  97. // 关闭
  98. bool m_close;
  99. };
  100. }
  101. }
  102. }
  103. #endif