CurlClient.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef __CURLCLIENT__
  2. #define __CURLCLIENT__
  3. #include "curl/curl.h"
  4. #include "curl/easy.h"
  5. #include "curl/curlver.h"
  6. #include <string>
  7. #include <vector>
  8. using namespace std;
  9. #ifndef _UNICODE
  10. typedef string TString;
  11. #else
  12. typedef wstring TString;
  13. #endif
  14. #pragma once
  15. class CCurlClient
  16. {
  17. public:
  18. CCurlClient(void);
  19. ~CCurlClient(void);
  20. public:
  21. INT Initialize();
  22. /**
  23. * @brief HTTP POST请求
  24. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  25. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  26. * @param strResponse 输出参数,返回的内容
  27. * @return 返回是否Post成功
  28. */
  29. int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
  30. int Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen);
  31. #ifdef USE_MFC
  32. int Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse);
  33. #endif
  34. /**
  35. * @brief HTTP GET请求
  36. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  37. * @param strResponse 输出参数,返回的内容
  38. * @return 返回是否Post成功
  39. */
  40. int Get(const std::string & strUrl, std::string & strResponse);
  41. int Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen);
  42. #ifdef USE_MFC
  43. int Get(IN CString& strUrl, OUT CString& strResponse);
  44. #endif
  45. /**
  46. * @brief HTTPS POST请求,无证书版本
  47. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  48. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  49. * @param strResponse 输出参数,返回的内容
  50. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  51. * @return 返回是否Post成功
  52. */
  53. int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
  54. int Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL);
  55. #ifdef USE_MFC
  56. int Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath = _T(""));
  57. #endif
  58. /**
  59. * @brief HTTPS GET请求,无证书版本
  60. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  61. * @param strResponse 输出参数,返回的内容
  62. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  63. * @return 返回是否Post成功
  64. */
  65. int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
  66. int Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL);
  67. #ifdef USE_MFC
  68. int Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath = _T(""));
  69. #endif
  70. public:
  71. void SetDebug(bool bDebug);
  72. private:
  73. // 是否启用调试输出;
  74. BOOL m_bDebug;
  75. static size_t OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream);
  76. };
  77. #endif