#ifndef __CURLCLIENT__ #define __CURLCLIENT__ #include #include 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成功 */ int 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); 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); }; #endif