CurlClient.h 2.7 KB

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