#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")
//#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "winhttp.lib")
int main(int argc, char *argv[])
{
//DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
LPCWSTR acceptTypes[2] = {TEXT("text/xml ; charset=utf-8"), NULL};
DWORD data;
DWORD dwSize = sizeof(DWORD);
	LPCWSTR additionalHeaders = L"";
	DWORD hLen = sizeof(LPCWSTR) * wcslen(additionalHeaders)+1;
	LPVOID dwCert = NULL;
DWORD dwCertLen = NULL;
	LPSTR msg = "<Request>12345</Request>";
	//const void* msgToSend = (const void*)msg;
	DWORD data_len = sizeof(LPSTR) * strlen(msg);
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"my user agent",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Use WinHttpQueryOption to retrieve internet options.
if (WinHttpQueryOption( hSession,
WINHTTP_OPTION_CONNECT_TIMEOUT,
&data, &dwSize))
{
printf("Connection timeout: %u ms\n\n",data);
}
else
{
printf( "Error %u in WinHttpQueryOption.\n",
GetLastError());
}
// Specify an HTTP server.
if (hSession)
		hConnect = WinHttpConnect( hSession, L"172.155.155.122",
443, 0);
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest( hConnect, L"POST", L"/TEST",
NULL, WINHTTP_NO_REFERER,
acceptTypes,
WINHTTP_FLAG_SECURE);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest( hRequest,
NULL,
0, (LPVOID)msg, data_len,
data_len, WINHTTP_FLAG_SECURE);
	DWORD dwError = GetLastError();
if ( dwError == ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED )
{
		
//MY is the store the certificate is in.
	PCCERT_CONTEXT pCertContext = NULL;
	LPVOID szCertName = NULL;
HCERTSTORE hMyStore = CertOpenSystemStore( 0, TEXT("MY") );
if( hMyStore )
{
pCertContext = CertFindCertificateInStore( hMyStore,
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
0,
CERT_FIND_SUBJECT_STR,
(LPVOID) szCertName, //Subject string in the certificate.
NULL );
if( pCertContext )
{
WinHttpSetOption( hRequest,
WINHTTP_OPTION_CLIENT_CERT_CONTEXT,
(LPVOID) pCertContext,
sizeof(CERT_CONTEXT) );
CertFreeCertificateContext( pCertContext );
}
CertCloseStore( hMyStore, 0 );
// NOTE: Application should now resend the request.
	 }
}
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0, (LPVOID)msg, data_len,
data_len, WINHTTP_FLAG_SECURE);
	if (!bResults)
printf( "Error %d has occurred - WinHttpSendRequest.\n", GetLastError());
// End the request.
if (bResults)
bResults = WinHttpReceiveResponse( hRequest, NULL);
// Continue to verify data until there is nothing left.
if (bResults)
do
{
// Verify available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
printf( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize=0;
}
else
{
// Read the Data.
ZeroMemory(pszOutBuffer, dwSize+1);
if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf( "Error %u in WinHttpReadData.\n", GetLastError());
else
printf( "%s\n", pszOutBuffer);
// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}
} while (dwSize > 0);
// Report any errors.
if (!bResults)
printf( "Error %d has occurred - WinHttpReceiveResponse.\n", GetLastError());
// Close open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
	system("pause");
return( 0 );
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE