CurlClient.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef __CURLCLIENT__
  2. #define __CURLCLIENT__
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. #include "curl/curl.h"
  7. #include "curl/easy.h"
  8. #include "curl/curlver.h"
  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, long time_out = 3000);
  30. int Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
  31. int Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out = 3000);
  32. /**
  33. * @brief HTTP GET请求
  34. * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
  35. * @param strResponse 输出参数,返回的内容
  36. * @return 返回是否Post成功
  37. */
  38. int Get(const std::string & strUrl, std::string & strResponse, long time_out = 3000);
  39. int Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
  40. int Get(IN CString& strUrl, OUT CString& strResponse, long time_out = 3000);
  41. /**
  42. * @brief HTTPS POST请求,无证书版本
  43. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  44. * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
  45. * @param strResponse 输出参数,返回的内容
  46. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  47. * @return 返回是否Post成功
  48. */
  49. int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
  50. int Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
  51. int Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
  52. /**
  53. * @brief HTTPS GET请求,无证书版本
  54. * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
  55. * @param strResponse 输出参数,返回的内容
  56. * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
  57. * @return 返回是否Post成功
  58. */
  59. int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
  60. int Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
  61. int Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
  62. public:
  63. void SetDebug(bool bDebug);
  64. void SetHeaders(const std::string headers);
  65. void ClearHeaders()
  66. {
  67. curl_slist_free_all(m_headers);
  68. m_headers = NULL;
  69. }
  70. private:
  71. // 是否启用调试输出;
  72. BOOL m_bDebug;
  73. struct curl_slist *m_headers;
  74. static size_t OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream);
  75. };
  76. #endif