CurlClient.h 2.6 KB

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