CurlClient.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef __CURLCLIENT__
  2. #define __CURLCLIENT__
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. using namespace std;
  7. #include "curl/curl.h"
  8. #include "curl/easy.h"
  9. #include "curl/curlver.h"
  10. #ifndef _UNICODE
  11. typedef string TString;
  12. #else
  13. typedef wstring TString;
  14. #endif
  15. #pragma once
  16. typedef struct _URL_PARS_
  17. {
  18. std::string host;
  19. std::string api;
  20. std::map<std::string, std::string> pars;
  21. }UrlPars, *pUrlPars;
  22. class CCurlClient
  23. {
  24. public:
  25. CCurlClient(void);
  26. ~CCurlClient(void);
  27. public:
  28. BOOL IsInit() const {return m_bInit;};
  29. BOOL Initialize();
  30. /**
  31. * @brief HTTP POST请求
  32. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  33. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  34. * @param strResponse 输出参数,返回的内容
  35. * @return 返回是否Post成功
  36. */
  37. CURLcode Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, long time_out = 3000);
  38. int Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
  39. int Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out = 3000);
  40. /**
  41. * @brief HTTP GET请求
  42. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  43. * @param strResponse 输出参数,返回的内容
  44. * @return 返回是否Post成功
  45. */
  46. int Get(const std::string & strUrl, std::string & strResponse, long time_out = 3000);
  47. int Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
  48. int Get(IN CString& strUrl, OUT CString& strResponse, long time_out = 3000);
  49. /**
  50. * @brief HTTPS POST请求,无证书版本
  51. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  52. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  53. * @param strResponse 输出参数,返回的内容
  54. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  55. * @return 返回是否Post成功
  56. */
  57. CURLcode Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
  58. int Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
  59. int Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
  60. /**
  61. * @brief HTTPS GET请求,无证书版本
  62. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  63. * @param strResponse 输出参数,返回的内容
  64. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  65. * @return 返回是否Post成功
  66. */
  67. int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
  68. int Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
  69. int Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
  70. static void ParseURL(std::string url, UrlPars &pars);
  71. static std::string ParseURLAndEncode(std::string url);
  72. // 一次性下载;
  73. bool Download(const std::string& url, const std::string& path, long time_out = 3000);
  74. // 断点下载;
  75. bool DownloadEx(const std::string& url, const std::string& path, long time_out = 3000);
  76. int FormPosts(std::string url, std::multimap<std::string, std::string> form_data, std::string& result, long time_out);
  77. public:
  78. void SetDebug(bool bDebug);
  79. void SetHeaders(const std::string headers);
  80. void ClearHeaders()
  81. {
  82. curl_slist_free_all(m_headers);
  83. m_headers = NULL;
  84. }
  85. private:
  86. BOOL m_bInit;
  87. // 是否启用调试输出;
  88. BOOL m_bDebug;
  89. struct curl_slist *m_headers;
  90. static size_t OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream);
  91. static size_t OnWriteFile(const void* ptr, size_t size, size_t nmemb, void* stream);
  92. static size_t OnGetContentLength(void* ptr, size_t size, size_t nmemb, void* stream);
  93. public:
  94. // 非通用接口,根据具体项目实现;
  95. CURLcode PostFile(std::string url, std::string text, std::string file, std::string& result, long time_out = 3000);
  96. };
  97. #endif