#include "stdafx.h"
#include "httpsAndHttp.h"
#include "windows.h"
#include "winhttp.h"
#pragma comment(lib,"Winhttp.lib")
BOOL CBusinessAPI::HttpPostToBusinessServer(LPCTSTR serverAddr, std::string urlParam, std::string & strHttpContent, LPDWORD statusCode)
{
URL_COMPONENTS urlComponents;
ZeroMemory(&urlComponents, sizeof(urlComponents));
urlComponents.dwStructSize = sizeof(urlComponents);
TCHAR hostName[256];
urlComponents.lpszHostName = hostName;
urlComponents.dwHostNameLength = 256;
TCHAR path[1024];
urlComponents.lpszUrlPath = path;
urlComponents.dwUrlPathLength = 1024;
if (WinHttpCrackUrl(serverAddr, lstrlen(serverAddr), 0, &urlComponents) == FALSE)
{
return FALSE;
}
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
BOOL bResults = FALSE;
std::wstring httpHeader;
std::wstring strUserIDParam;
hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
goto failure;
}
hConnect = WinHttpConnect(hSession, urlComponents.lpszHostName, urlComponents.nPort, 0);
if (!hConnect)
{
goto failure;
}
strUserIDParam = urlComponents.lpszUrlPath;
//secure ? WINHTTP_FLAG_SECURE|WINHTTP_FLAG_REFRESH : WINHTTP_FLAG_REFRESH
hRequest = WinHttpOpenRequest(hConnect, L"POST", strUserIDParam.c_str(), L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
if (!hRequest)
{
goto failure;
}
httpHeader = L"Content-type: application/json";
WinHttpAddRequestHeaders(hRequest, httpHeader.c_str(), DWORD(httpHeader.length()), WINHTTP_ADDREQ_FLAG_ADD);
if (!hRequest)
{
goto failure;
}
const void *ss = (const char *)urlParam.c_str();
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, const_cast<void *>(ss), urlParam.length(), urlParam.length(), 0);
if (bResults)
{
bResults = WinHttpReceiveResponse(hRequest, NULL);
}
else
{
goto failure;
}
DWORD statusCodeSize = sizeof(DWORD);
bResults = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, statusCode, &statusCodeSize, WINHTTP_NO_HEADER_INDEX);
if (!bResults)
{
goto failure;
}
strHttpContent.clear();
if (bResults)
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
DWORD dwReadBytes = 0;
do
{
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
goto failure;
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
dwSize = 0;
}
else
{
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
goto failure;
}
strHttpContent += pszOutBuffer;
delete[] pszOutBuffer;
}
} while (dwSize > 0);
}
if (strHttpContent.empty())
{
goto failure;
}
else
{
if (hSession)
WinHttpCloseHandle(hSession);
if (hConnect)
WinHttpCloseHandle(hConnect);
if (hRequest)
WinHttpCloseHandle(hRequest);
return TRUE;
}
failure:
if (hSession)
WinHttpCloseHandle(hSession);
if (hConnect)
WinHttpCloseHandle(hConnect);
if (hRequest)
WinHttpCloseHandle(hRequest);
return FALSE;
}
BOOL CBusinessAPI::HttpDeleteToBusinessServer(LPCTSTR serverAddr, std::string urlParam, std::string & strHttpContent, LPDWORD statusCode)
{
URL_COMPONENTS urlComponents;
ZeroMemory(&urlComponents, sizeof(urlComponents));
urlComponents.dwStructSize = sizeof(urlComponents);
TCHAR hostName[256];
urlComponents.lpszHostName = hostName;
urlComponents.dwHostNameLength = 256;
TCHAR path[1024];
urlComponents.lpszUrlPath = path;
urlComponents.dwUrlPathLength = 1024;
if (WinHttpCrackUrl(serverAddr, lstrlen(serverAddr), 0, &urlComponents) == FALSE)
{
return FALSE;
}
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
BOOL bResults = FALSE;
std::wstring httpHeader;
std::wstring strUserIDParam;
hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
goto failure;
}
hConnect = WinHttpConnect(hSession, urlComponents.lpszHostName, urlComponents.nPort, 0);
if (!hConnect)
{
goto failure;
}
strUserIDParam = urlComponents.lpszUrlPath;
hRequest = WinHttpOpenRequest(hConnect, L"DELETE", strUserIDParam.c_str(), L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
if (!hRequest)
{
goto failure;
}
httpHeader = L"Content-type: application/json";
WinHttpAddRequestHeaders(hRequest, httpHeader.c_str(), DWORD(httpHeader.length()), WINHTTP_ADDREQ_FLAG_ADD);
if (!hRequest)
{
goto failure;
}
const void *ss = (const char *)urlParam.c_str();
bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, const_cast<void *>(ss), urlParam.length(), urlParam.length(), 0);
if (bResults)
{
bResults = WinHttpReceiveResponse(hRequest, NULL);
}
else
{
goto failure;
}
DWORD statusCodeSize = sizeof(DWORD);
bResults = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, statusCode, &statusCodeSize, WINHTTP_NO_HEADER_INDEX);
if (!bResults)
{
goto failure;
}
strHttpContent.clear();
if (bResults)
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
DWORD dwReadBytes;
do
{
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
goto failure;
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
dwSize = 0;
}
else
{
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
goto failure;
}
strHttpContent += pszOutBuffer;
delete[] pszOutBuffer;
}
} while (dwSize > 0);
}
if (*statusCode != 200)
{
goto failure;
}
else
{
if (hSession)
WinHttpCloseHandle(hSession);
if (hConnect)
WinHttpCloseHandle(hConnect);
if (hRequest)
WinHttpCloseHandle(hRequest);
return TRUE;
}
failure:
if (hSession)
WinHttpCloseHandle(hSession);
if (hConnect)
WinHttpCloseHandle(hConnect);
if (hRequest)
WinHttpCloseHandle(hRequest);
return FALSE;
return 0;
}
BOOL CBusinessAPI::HttpPutToBusinessServer(LPCTSTR serverAddr, std::string urlParam, std::string & strHttpContent, LPDWORD statusCode)
{
URL_COMPONENTS urlComponents;
ZeroMemory(&urlComponents, sizeof(urlComponents));
urlComponents.dwStructSize = sizeof(urlComponents);
TCHAR hostName[256];
urlComponents.lpszHostName = hostName;
urlComponents.dwHostNameLength = 256;
TCHAR path[1024];
urlComponents.lpszUrlPath = path;
urlComponents.dwUrlPathLength = 1024;
if (WinHttpCrackUrl(serverAddr, lstrlen(serverAddr), 0, &urlComponents) == FALSE)
{
return FALSE;
}
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
BOOL bResults = FALSE;
std::wstring httpHeader;
std::wstring strUserIDParam;
hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
goto failure;
}
hConnect = WinHttpConnect(hSession, urlComponents.lpszHostName, urlComponents.nPort, 0);
if (!hConnect)
{
goto failure;
}
strUserIDParam = urlComponents.lpszUrlPath;
hRequest = WinHttpOpenRequest(hConnect, L"PUT", strUserIDParam.c_str(), L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_REFRESH);
if (!hRequest)
{
goto failure;
}
httpHeader = L"Content-type: application/json";
WinHttpAddRequestHeaders(hRequest, httpHeader.c_str(), DWORD(httpHeader.length()), WINHTTP_ADDREQ_FLAG_ADD);
if (!hRequest)
{
goto f