C++封装的HTTP和HTTPS的接口

记录下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;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值