http_client.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef HTTP_CLIENT_H
  2. #define HTTP_CLIENT_H
  3. #include "curl/curl.h"
  4. #include <string>
  5. #include <list>
  6. #include <map>
  7. #if defined(_WIN32) || defined(_WIN64)
  8. #pragma comment(lib, "Wldap32.lib")
  9. #pragma comment (lib, "Advapi32.lib")
  10. #pragma comment(lib, "Ws2_32.lib")
  11. #pragma comment(lib, "User32.lib")
  12. #pragma comment(lib, "Gdi32.lib")
  13. #endif
  14. using std::string;
  15. using std::list;
  16. using std::map;
  17. #ifdef _DEBUG
  18. #define DebugLog(format,...) printf("[" __FILE__ ":%04d]\n" format "\n\n", __LINE__, ##__VA_ARGS__)
  19. #else
  20. #define DebugLog(format,...)
  21. #endif
  22. typedef map<string, string> HeaderMap;
  23. typedef map<string, string> ParamsMap;
  24. /**
  25. * HttpClient
  26. * @brief The HttpClient class
  27. */
  28. class HttpClient
  29. {
  30. public:
  31. HttpClient();
  32. HttpClient(const HttpClient &);
  33. HttpClient & operator = (const HttpClient &);
  34. ~HttpClient();
  35. public:
  36. /**
  37. * Set global proxy
  38. * 设置全局代理
  39. * @brief setProxy
  40. * @param proxyIp
  41. * @param proxyPort
  42. * @param proxyUserName
  43. * @param proxyPassword
  44. */
  45. static void setProxy(const string &proxyIp = string(),
  46. const string &proxyPort = string(),
  47. const string &proxyUserName = string(),
  48. const string &proxyPassword = string());
  49. private:
  50. static string proxyIp;
  51. static string proxyPort;
  52. static string proxyUserName;
  53. static string proxyPassword;
  54. private:
  55. string responseEntity;
  56. public:
  57. /**
  58. * @brief sendSyncRequest
  59. * @param url
  60. * @param requestEntity
  61. * @param headers
  62. * @return
  63. */
  64. string sendSyncRequest(const string &url,
  65. const string &requestEntity,
  66. const HeaderMap &headers = HeaderMap());
  67. /**
  68. * @brief sendSyncRequest
  69. * @param url
  70. * @param paramsMap
  71. * @param headers
  72. * @return
  73. */
  74. string sendSyncRequest(const string &url,
  75. const ParamsMap &paramsMap,
  76. const HeaderMap &headers = HeaderMap());
  77. private:
  78. CURL *curl;
  79. static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
  80. };
  81. #endif // HTTP_CLIENT_H