123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #ifndef __CURLCLIENT__
- #define __CURLCLIENT__
- #include <string>
- #include <vector>
- using namespace std;
- #include "curl/curl.h"
- #include "curl/easy.h"
- #include "curl/curlver.h"
- #ifndef _UNICODE
- typedef string TString;
- #else
- typedef wstring TString;
- #endif
- #pragma once
- class CCurlClient
- {
- public:
- CCurlClient(void);
- ~CCurlClient(void);
- public:
- INT Initialize();
- /**
- * @brief HTTP POST请求
- * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
- * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
- * @param strResponse 输出参数,返回的内容
- * @return 返回是否Post成功
- */
- int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse, long time_out = 3000);
- int Post(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
- int Post(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, long time_out = 3000);
- /**
- * @brief HTTP GET请求
- * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com
- * @param strResponse 输出参数,返回的内容
- * @return 返回是否Post成功
- */
- int Get(const std::string & strUrl, std::string & strResponse, long time_out = 3000);
- int Get(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, long time_out = 3000);
- int Get(IN CString& strUrl, OUT CString& strResponse, long time_out = 3000);
- /**
- * @brief HTTPS POST请求,无证书版本
- * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
- * @param strPost 输入参数,使用如下格式para1=val1?2=val2&…
- * @param strResponse 输出参数,返回的内容
- * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
- * @return 返回是否Post成功
- */
- CURLcode Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
- int Posts(IN LPCTSTR lpUrl, IN LPCTSTR lpPost, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
- int Posts(IN CString& strUrl, IN CString& strPost, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
- /**
- * @brief HTTPS GET请求,无证书版本
- * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
- * @param strResponse 输出参数,返回的内容
- * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
- * @return 返回是否Post成功
- */
- int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL, long time_out = 3000);
- int Gets(IN LPCTSTR lpUrl, OUT LPTSTR lpResponse, IN CONST INT& nMaxlen, IN LPCTSTR lpCaPath = NULL, long time_out = 3000);
- int Gets(IN CString& strUrl, OUT CString& strResponse, IN const CString& strCaPath = _T(""), long time_out = 3000);
- bool DownloadEx(const std::string& url, const std::string& path, long time_out = 3000);
- int FormPosts(std::string url, std::multimap<std::string, std::string> form_data, std::string& result, long time_out);
- public:
- void SetDebug(bool bDebug);
- void SetHeaders(const std::string headers);
- void ClearHeaders()
- {
- curl_slist_free_all(m_headers);
- m_headers = NULL;
- }
- private:
- // 是否启用调试输出;
- BOOL m_bDebug;
- struct curl_slist *m_headers;
- static size_t OnWriteData(const void *ptr, size_t size, size_t nmemb, std::string *stream);
- static size_t OnWriteFile(const void* ptr, size_t size, size_t nmemb, void* stream);
- static size_t OnGetContentLength(void* ptr, size_t size, size_t nmemb, void* stream);
- };
- #endif
|