http_client_plus.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /// <summary>
  31. /// 连接代理服务器
  32. /// </summary>
  33. /// <param name="address"></param>
  34. /// <param name="port"></param>
  35. /// <returns></returns>
  36. void setproxy(const std::string& address,ushort port);
  37. network::http::header_list& headers_request();
  38. network::http::header_list& headers_response();
  39. uint32 status();
  40. ylib::buffer& response();
  41. /*设置缓存*/
  42. void cache(client_cache* cache);
  43. /*请求路径*/
  44. inline const std::string& url() { return m_url; }
  45. /*cookie*/
  46. inline network::http::cookie& cookie() { return m_cookie; }
  47. inline void cookie(const network::http::cookie& ck) { m_cookie = ck; }
  48. /*[回调] 正在下载*/
  49. void on_down_ing(const std::function<bool(void* data, uint32 downsize, uint64 alldownsize, uint64 allsize, network::http::client_plus& client)>& callback);
  50. /*[回调] 下载结束*/
  51. void on_down_end(const std::function<void(network::http::client_plus& client)>& callback);
  52. /*[回调] 下载失败*/
  53. void on_down_failed(const std::function<void(network::http::client_plus& client)>& callback);
  54. // 清理所有Cookie缓存
  55. static void clear_all_cookies();
  56. friend class http_client_listener;
  57. public:
  58. uint64 m_temp[10];
  59. private:
  60. bool parseurl(std::string url);
  61. bool connect();
  62. bool init();
  63. bool request();
  64. bool post(const std::string& url);
  65. void* client();
  66. bool init_proxy();
  67. private:
  68. // HP客户端
  69. void* m_client;
  70. // HP客户端-SSL
  71. void* m_client_ssl;
  72. // HP监听器
  73. http_client_listener* m_listener;
  74. // 是否已初始化
  75. bool m_init;
  76. // 连接IP
  77. std::string m_ipaddress;
  78. // 连接端口
  79. ushort m_port;
  80. // HTTPS
  81. bool m_ssl;
  82. // 请求链接
  83. std::string m_url;
  84. // 请求路径
  85. std::string m_path;
  86. // 请求方式
  87. std::string m_method;
  88. // 请求数据
  89. ylib::buffer m_request_body;
  90. // [header] 请求
  91. network::http::header_list m_headers_request;
  92. // 超时时间 连接
  93. timestamp m_timeout_connect_msec;
  94. // 超时时间 接收
  95. timestamp m_timeout_recv_msec;
  96. // 缓存
  97. client_cache* m_cache;
  98. // cookie
  99. network::http::cookie m_cookie;
  100. // 请求等待
  101. bool m_request_wait = true;
  102. #ifndef _WIN32
  103. public:
  104. #endif
  105. // 关闭
  106. bool m_close;
  107. // 代理服务器
  108. ylib::AddressPort m_proxy;
  109. };
  110. }
  111. }
  112. }
  113. #endif