CurlClient.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. class CCurlClient
  17. {
  18. public:
  19. CCurlClient(void);
  20. ~CCurlClient(void);
  21. public:
  22. INT Initialize();
  23. /**
  24. * @brief HTTP POST请求
  25. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  26. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  27. * @param strResponse 输出参数,返回的内容
  28. * @return 返回是否Post成功
  29. */
  30. int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, long time_out = 3000);
  31. int Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
  32. int Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out = 3000);
  33. /**
  34. * @brief HTTP GET请求
  35. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  36. * @param strResponse 输出参数,返回的内容
  37. * @return 返回是否Post成功
  38. */
  39. int Get(const std::string & strUrl, std::string & strResponse, long time_out = 3000);
  40. int Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
  41. int Get(IN CString& strUrl, OUT CString& strResponse, long time_out = 3000);
  42. /**
  43. * @brief HTTPS POST请求,无证书版本
  44. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  45. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  46. * @param strResponse 输出参数,返回的内容
  47. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  48. * @return 返回是否Post成功
  49. */
  50. int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
  51. int Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
  52. int Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
  53. /**
  54. * @brief HTTPS GET请求,无证书版本
  55. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  56. * @param strResponse 输出参数,返回的内容
  57. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  58. * @return 返回是否Post成功
  59. */
  60. int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
  61. int Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
  62. int Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
  63. bool Download(const std::string &url, const std::string &path, long time_out = 3000);
  64. bool DownloadEx(const std::string &url, const std::string &path, long time_out = 3000);
  65. int FormPost(std::string url, std::multimap<std::string, std::string> form_data, std::string &result, long time_out = 3000);
  66. int FormPosts(std::string url, std::multimap<std::string, std::string> form_data, std::string &result, long time_out = 3000);
  67. public:
  68. void SetDebug(bool bDebug);
  69. void SetHeaders(const std::string headers);
  70. void ClearHeaders()
  71. {
  72. if (m_headers)
  73. curl_slist_free_all(m_headers);
  74. m_headers = NULL;
  75. }
  76. private:
  77. // 是否启用调试输出;
  78. BOOL m_bDebug;
  79. struct curl_slist *m_headers;
  80. static size_t OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream);
  81. static size_t OnWriteFile(const void *ptr, size_t size, size_t nmemb, void *stream);
  82. static size_t OnGetContentLength(void *ptr, size_t size, size_t nmemb, void *stream);
  83. };
  84. #endif