//////////////////////////////////// HttpClient.h #ifndef HTTPCLIENT_H #define HTTPCLIENT_H #include "StdAfx.h" #include #include #include #include #include #define IE_AGENT _T("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)") // 操作成功 #define SUCCESS 0 // 操作失败 #define FAILURE 1 #define OUTTIME 2 class CAttemperEngineSink; class CHttpClient { public: CHttpClient(LPCTSTR strAgent = IE_AGENT); virtual ~CHttpClient(void); int HttpGet(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse, CString &Err); int HttpPost(LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse, CString &Err); //添加 static void AddLog(DWORD dwSocketID, GETDUILIEBASE_EX LogText); //获取 static std::unordered_map GetLog(); //运行 static unsigned __stdcall Runs(void* pParam); //启动 static bool start(CAttemperEngineSink* pSink); //结束 static bool end(); //获取数量 static DWORD GetCount(); // 进行Url编码 UTF-8 static CString UrlEncode(CString strUnicode); private: int ExecuteRequest(LPCTSTR strMethod, LPCTSTR strUrl, LPCTSTR strPostData, CString &strResponse,CString &Err); void Clear(); static HANDLE m_hThread; // 线程句柄 static unsigned m_uiThreadID; // 线程id static bool m_bRun; // 当前线程是否结束 static HANDLE g_Event; // 触发事件句柄 static CAttemperEngineSink* pCAttemperEngineSink; //调度钩子 static void* curl; //请求信息 static std::unordered_map GetData; //请求信息 //锁 static CCriticalSection m_CriticalSection; //线程锁 private: CInternetSession *m_pSession; CHttpConnection *m_pConnection; CHttpFile *m_pFile; public: //获取验证码 static bool OnGetIphoneNumber(CString csIphoneNum, DWORD dwUserID, DWORD dwSocketID); public: /** * @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); /** * @brief HTTP GET请求 * @param strUrl 输入参数,请求的Url地址,如:http://www.baidu.com * @param strResponse 输出参数,返回的内容 * @return 返回是否Post成功 */ int Get(const std::string & strUrl, std::string & strResponse); /** * @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); /** * @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); public: void SetDebug(bool bDebug); private: bool m_bDebug; }; #endif // HTTPCLIENT_H