CurlClient.h 3.8 KB

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