CurlClient.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. CURLcode 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. bool DownloadEx(const std::string& url, const std::string& path, long time_out = 3000);
  63. int FormPosts(std::string url, std::multimap<std::string, std::string> form_data, std::string& result, long time_out);
  64. public:
  65. void SetDebug(bool bDebug);
  66. void SetHeaders(const std::string headers);
  67. void ClearHeaders()
  68. {
  69. curl_slist_free_all(m_headers);
  70. m_headers = NULL;
  71. }
  72. private:
  73. // 是否启用调试输出;
  74. BOOL m_bDebug;
  75. struct curl_slist *m_headers;
  76. static size_t OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream);
  77. static size_t OnWriteFile(const void* ptr, size_t size, size_t nmemb, void* stream);
  78. static size_t OnGetContentLength(void* ptr, size_t size, size_t nmemb, void* stream);
  79. };
  80. #endif