记录下http和https的post方法 在C++中实现
来源无从考究了,这里记录一下
http 的post 方法
BOOL HttpPostToBusinessServer(LPCTSTR serverAddr, std::string urlParam, std::string &strHttpContent, LPDWORD statusCode);
BOOL 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;
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;
}
https 的 post方法
BOOL HttpsPostToBusinessServer(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"POST", strUserIDParam.c_str(), L"HTTP/1.1", WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH);
if (!hRequest)
{
goto failure;
}
DWORD dwFlags;
DWORD dwBuffLen = sizeof(dwFlags);
WinHttpQueryOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS,
(LPVOID)&dwFlags, &dwBuffLen);
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
dwFlags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
dwFlags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
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;
}