123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- #include "stdafx.h"
- #include "HttpClientSyn.h"
- #include <atlbase.h>
- #include <atlconv.h>
- #pragma comment(lib,"Winhttp.lib")
- #define MAXSTATICPARAMCOUNT 32
- CHttpClientSyn::CHttpClientSyn()
- {
- m_hSession = NULL;
- m_hConnect = NULL;
- m_hRequest = NULL;
- m_dwTimeout = 0;
- m_lpReceiveData = NULL;
- m_dwReceiveDataLength = 0;
- }
- CHttpClientSyn::~CHttpClientSyn(void)
- {
- ClearEvn();
- }
- BOOL CHttpClientSyn::InitializeHttp(const std::wstring& wstrUrl, DWORD dwTimeout)
- {
- static URL_COMPONENTS urlCom;
- memset(&urlCom, 0, sizeof(urlCom));
- urlCom.dwStructSize = sizeof(urlCom);
- static WCHAR wchScheme[64] = { 0 };
- memset(wchScheme, 0, 64 * sizeof(WCHAR));
- urlCom.lpszScheme = wchScheme;
- urlCom.dwSchemeLength = ARRAYSIZE(wchScheme);
- static WCHAR wchHostName[1024] = { 0 };
- memset(wchHostName, 0, 1024 * sizeof(WCHAR));
- urlCom.lpszHostName = wchHostName;
- urlCom.dwHostNameLength = ARRAYSIZE(wchHostName);
- static WCHAR wchUrlPath[1024] = { 0 };
- memset(wchUrlPath, 0, 1024 * sizeof(WCHAR));
- urlCom.lpszUrlPath = wchUrlPath;
- urlCom.dwUrlPathLength = ARRAYSIZE(wchUrlPath);
- static WCHAR wchExtraInfo[4096] = { 0 };
- memset(wchExtraInfo, 0, 4096 * sizeof(WCHAR));
- urlCom.lpszExtraInfo = wchExtraInfo;
- urlCom.dwExtraInfoLength = ARRAYSIZE(wchExtraInfo);
- if (FALSE == WinHttpCrackUrl(wstrUrl.c_str(), wstrUrl.length(), ICU_ESCAPE, &urlCom))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- static std::wstring wstrExtraInfo;
- wstrExtraInfo = urlCom.lpszExtraInfo;
- ParseParams(wstrExtraInfo);
- AddExtInfo(m_VecExtInfo);
- if (m_hSession == NULL)
- m_hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
- if (NULL == m_hSession)
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- if (FALSE == WinHttpSetTimeouts(m_hSession, dwTimeout, dwTimeout, dwTimeout, dwTimeout))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- if (m_hConnect == NULL)
- m_hConnect = WinHttpConnect(m_hSession, urlCom.lpszHostName, urlCom.nPort, 0);
- if (NULL == m_hConnect)
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- m_wstrUrlPath = urlCom.lpszUrlPath;
- return TRUE;
- }
- VOID CHttpClientSyn::UninitializeHttp()
- {
- if (NULL != m_hConnect)
- {
- WinHttpCloseHandle(m_hConnect);
- m_hConnect = NULL;
- }
- if (NULL != m_hRequest)
- {
- WinHttpCloseHandle(m_hRequest);
- m_hRequest = NULL;
- }
- if (NULL != m_hSession)
- {
- WinHttpCloseHandle(m_hSession);
- m_hSession = NULL;
- }
- m_VecExtInfo.clear();
- }
- BOOL CHttpClientSyn::ReceiveData()
- {
- BOOL bSuc = FALSE;
- DWORD dwReceivedBufferLength = 0;
- LPBYTE lpReceivedBuffer = NULL;
- if (FALSE == WinHttpReceiveResponse(m_hRequest, NULL))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- DWORD dwRetLength = 0;
- do {
- bSuc = FALSE;
- if (FALSE == WinHttpQueryDataAvailable(m_hRequest, &dwRetLength))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- break;
- }
- if (0 == dwRetLength)
- {
- bSuc = TRUE;
- break;
- }
- LPBYTE lpReceivedData = new BYTE[dwRetLength];
- if (NULL == lpReceivedData)
- {
- break;
- }
- memset(lpReceivedData, 0, dwRetLength);
- DWORD dwRead = 0;
- if (FALSE == WinHttpReadData(m_hRequest, lpReceivedData, dwRetLength, &dwRead))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- break;
- }
- if (0 == dwRead)
- {
- break;
- }
- DWORD dwReceivedBufferLengthNew = dwReceivedBufferLength + dwRetLength;
- LPBYTE lpReceivedBufferNew = new BYTE[dwReceivedBufferLengthNew];
- if (NULL == lpReceivedBufferNew)
- {
- break;
- }
- memset(lpReceivedBufferNew, 0, dwReceivedBufferLengthNew);
- if (NULL != lpReceivedBuffer)
- {
- memcpy_s(lpReceivedBufferNew, dwReceivedBufferLengthNew, lpReceivedBuffer, dwReceivedBufferLength);
- delete[] lpReceivedBuffer;
- lpReceivedBuffer = NULL;
- }
- if (NULL != lpReceivedData)
- {
- memcpy_s(lpReceivedBufferNew + dwReceivedBufferLength, dwReceivedBufferLengthNew - dwReceivedBufferLength, lpReceivedData, dwRetLength);
- delete[] lpReceivedData;
- lpReceivedData = NULL;
- }
- lpReceivedBuffer = lpReceivedBufferNew;
- dwReceivedBufferLength = dwReceivedBufferLengthNew;
- bSuc = TRUE;
- } while (dwRetLength > 0);
- if (bSuc)
- {
- m_lpReceiveData = lpReceivedBuffer;
- m_dwReceiveDataLength = dwReceivedBufferLength;
- m_strErrorDescriptor = _T("");
- }
- return TRUE;
- }
- BOOL CHttpClientSyn::ReceiveData(LPBYTE lpBuffer, DWORD& dwBufferSize)
- {
- if (NULL == m_lpReceiveData)
- {
- ::SetLastError(ERROR_WINHTTP_NOT_INITIALIZED);
- return FALSE;
- }
- if (NULL == lpBuffer)
- {
- ::SetLastError(ERROR_INVALID_ADDRESS);
- return FALSE;
- }
- if (m_dwReceiveDataLength > dwBufferSize)
- {
- dwBufferSize = m_dwReceiveDataLength;
- ::SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return FALSE;
- }
- errno_t e = memcpy_s(lpBuffer, dwBufferSize, m_lpReceiveData, m_dwReceiveDataLength);
- if (0 != e)
- {
- return FALSE;
- }
- ClearEvn();
- return TRUE;
- }
- VOID CHttpClientSyn::ClearEvn()
- {
- UninitializeHttp();
- m_dwTimeout = 0;
- if (NULL != m_lpReceiveData)
- {
- delete[] m_lpReceiveData;
- m_lpReceiveData = NULL;
- }
- m_dwReceiveDataLength = 0;
- }
- BOOL CHttpClientSyn::TransmiteData(const std::wstring& wstrUrl, EType eType, DWORD dwTimeout)
- {
- if (FALSE == InitializeHttp(wstrUrl, dwTimeout))
- {
- UninitializeHttp();
- return FALSE;
- }
- if (FALSE == TransmiteData(eType))
- {
- UninitializeHttp();
- return FALSE;
- }
- ReceiveData();
- UninitializeHttp();
- return TRUE;
- }
- BOOL CHttpClientSyn::TransmiteData(EType eType)
- {
- BOOL bSuc = FALSE;
- switch (eType)
- {
- case eGet:
- {
- bSuc = TransmiteDataToServerByGet();
- }
- break;
- case ePost:
- {
- bSuc = TransmiteDataToServerByPost();
- }
- break;
- case eUpload:
- {
- bSuc = TransmiteDataToServerByUpload();
- }
- break;
- default: break;
- }
- return bSuc;
- }
- BOOL CHttpClientSyn::GetData(LPVOID lpBuffer, DWORD dwBufferSize, DWORD& dwWrite)
- {
- return FALSE;
- }
- BOOL CHttpClientSyn::ModifyRequestHeader(HINTERNET hRequest)
- {
- return TRUE;
- }
- VOID CHttpClientSyn::ParseParams(const std::wstring& wstrExtraInfo)
- {
- static int nPos = 0;
- nPos = wstrExtraInfo.find('?');
- if (-1 == nPos)
- {
- return;
- }
- static std::wstring wstrParam;
- wstrParam = wstrExtraInfo;
- int nStaticMaxParamCount = MAXSTATICPARAMCOUNT;
- do{
- wstrParam = wstrParam.substr(nPos + 1, wstrExtraInfo.length() - nPos - 1);
- nPos = wstrParam.find('&', nPos);
- static std::wstring wstrKeyValuePair;
- if (-1 == nPos)
- {
- wstrKeyValuePair = wstrParam;
- }
- else
- {
- wstrKeyValuePair = wstrParam.substr(0, nPos);
- }
- int nSp = wstrKeyValuePair.find('=');
- if (-1 != nSp)
- {
- StParam stParam;
- stParam.wstrKey = wstrKeyValuePair.substr(0, nSp);
- stParam.wstrValue = wstrKeyValuePair.substr(nSp + 1, wstrKeyValuePair.length() - nSp - 1);
- m_VecExtInfo.push_back(stParam);
- }
- } while (-1 != nPos && nStaticMaxParamCount > 0);
- }
- BOOL CHttpClientSyn::TransmiteDataToServerByGet()
- {
- static std::wstring wstrUrlPathAppend;
- wstrUrlPathAppend = m_wstrUrlPath;
-
- if (false == wstrUrlPathAppend.empty())
- {
- wstrUrlPathAppend += L"?";
- }
- wstrUrlPathAppend += GenerateExtInfo(m_VecExtInfo);
- m_hRequest = WinHttpOpenRequest(m_hConnect, L"GET", wstrUrlPathAppend.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
- if (NULL == m_hRequest)
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- ModifyRequestHeader(m_hRequest);
- if (FALSE == WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- return TRUE;
- }
- BOOL CHttpClientSyn::TransmiteDataToServerByPost()
- {
- m_hRequest = WinHttpOpenRequest(m_hConnect, L"POST", m_wstrUrlPath.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
- if (NULL == m_hRequest)
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- ModifyRequestHeader(m_hRequest);
- std::wstring wstrExtInfo = GenerateExtInfo(m_VecExtInfo);
- std::string strExtInfo = CW2A(wstrExtInfo.c_str(), CP_UTF8);
- DWORD dwTotal = strExtInfo.length();
- dwTotal += GetDataSize();
- if (FALSE == WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, dwTotal, 0))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- if (0 != strExtInfo.length())
- {
-
- if (FALSE == WinHttpWriteData(m_hRequest, strExtInfo.c_str(), strExtInfo.length(), NULL))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- }
-
- BYTE buffer[1024] = { 0 };
- BOOL bContinue = FALSE;
- BOOL bSendOK = FALSE;
- do {
- DWORD dwBufferLength = sizeof(buffer);
- SecureZeroMemory(buffer, dwBufferLength);
- DWORD dwWriteSize = 0;
- bContinue = GetData(buffer, dwBufferLength, dwWriteSize);
- if (0 != dwWriteSize)
- {
- bSendOK = WinHttpWriteData(m_hRequest, buffer, dwWriteSize, NULL);
- }
- else
- {
- bSendOK = TRUE;
- }
- } while (bContinue && bSendOK);
- return bSendOK;
- }
- BOOL CHttpClientSyn::TransmiteDataToServerByUpload()
- {
- m_hRequest = WinHttpOpenRequest(m_hConnect, L"POST", m_wstrUrlPath.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
- if (NULL == m_hRequest)
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- ModifyRequestHeader(m_hRequest);
- std::wstring wstrExtInfo = GenerateExtInfo(m_VecExtInfo);
- std::string strExtInfo = CW2A(wstrExtInfo.c_str(), CP_UTF8);
- DWORD dwTotal = strExtInfo.length();
- dwTotal += GetDataSize();
- if (FALSE == WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, dwTotal, 0))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
-
- BYTE buffer[1024] = { 0 };
- BOOL bContinue = FALSE;
- BOOL bSendOK = FALSE;
- do {
- DWORD dwBufferLength = sizeof(buffer);
- SecureZeroMemory(buffer, dwBufferLength);
- DWORD dwWriteSize = 0;
- bContinue = GetData(buffer, dwBufferLength, dwWriteSize);
- if (0 != dwWriteSize)
- {
- bSendOK = WinHttpWriteData(m_hRequest, buffer, dwWriteSize, NULL);
- }
- else
- {
- bSendOK = TRUE;
- }
- } while (bContinue && bSendOK);
- if (0 != strExtInfo.length())
- {
- if (FALSE == WinHttpWriteData(m_hRequest, strExtInfo.c_str(), strExtInfo.length(), NULL))
- {
- m_strErrorDescriptor = GetLastErrorInfo(GetLastError());
- return FALSE;
- }
- }
- return bSendOK;
- }
|