#pragma once
#include "CHttpHeader.h"
using namespace std;
#define CONTENTTYPE_JSON L"Content-Type: application/json;"
#define CONTENTTYPE_TEXT L"Content-Type: text/html;"
#define CONTENTTYPE_URLENCODED L"Content-Type: application/x-www-form-urlencoded;charset:utf-8;"
class HttpClient
{
public:
	HttpClient(void);
	~HttpClient(void);

	void	SetTimeOut(int dwConnectTime, int dwSendTime, int dwRecvTime);
	string	Request(LPCSTR lpUrl, HttpRequestType type, void* lpPostData = NULL, LPCSTR lpHeader = NULL);
	string	Request(LPCWSTR lpUrl, HttpRequestType type, void* lpPostData = NULL, int datelen = 0, LPCWSTR lpHeader = NULL);
	string	Get(LPCWSTR lpUrl);
	string	Post(LPCWSTR lpUrl,const void* lpPostData = NULL, int datelen = 0, LPCWSTR lpHeader = NULL);
	void	FreeInstance() { delete this; }
	bool	DownloadFile(LPCWSTR lpUrl, LPCWSTR lpFilePath);
	bool	DownloadToMem(LPCWSTR lpUrl, OUT void** ppBuffer, OUT int* nSize);
	void	SetDownloadCallback(IHttpCallback* pCallback, void* pParam);
	HttpInterfaceError GetErrorCode() { return m_paramsData.errcode; }
	void AddHeader(LPCSTR key, LPCSTR value);
	int GetResponseCode() { return m_nResponseCode; }

protected:
	bool	Init();
	void	Release();
	//init
	bool	InitConnect(LPCWSTR lpUrl, HttpRequestType type, void* lpPostData = NULL, int datelen = 0, LPCWSTR lpHeader = NULL);
	bool	ConnectHttpServer(LPCWSTR lpIP, WORD wPort);
	bool	CreateHttpRequest(LPCWSTR lpPage, HttpRequestType type, DWORD dwFlag = 0);
	bool	SendHttpRequest(void* lpPostData = NULL, int datelen = 0, LPCWSTR lpHeader = NULL);
	//query 
	bool	QueryRawHeaders(OUT wstring& strHeaders);
	bool	QueryContentLength(OUT DWORD& dwLength);
	int  QueryStatusCode();

private:
	int m_nResponseCode;
	bool		m_bHttps;
	HINTERNET	m_hInternet;
	HINTERNET	m_hConnect;
	HINTERNET	m_hRequest;
	int			m_nConnTimeout;
	int			m_nSendTimeout;
	int			m_nRecvTimeout;
	CHttpHeader m_header;
	HttpParamsData m_paramsData;
};

