#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#include <string.h>
#pragma comment(lib, "Winhttp.lib")
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
DWORD dwError = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
std::wstring strUserAgent;
strUserAgent = L"NETIQ AppManger";
hSession = WinHttpOpen( strUserAgent.c_str(),
WINHTTP_ACCESS_TYPE_NO_PROXY,
NULL,
NULL, 0);
BOOL bReturn = WinHttpSetTimeouts(hSession,
12000,
12000,
12000,
12000);
if (hSession)
hConnect = WinHttpConnect( hSession, L"cucm11-5",
8443, 0);
if (hConnect)
hRequest = WinHttpOpenRequest( hConnect, L"GET", L"ccmadmin",
L"HTTP/1.1", NULL,
NULL,
WINHTTP_FLAG_SECURE);
unsigned long ulngSecurityOptions = 0;
unsigned long ulngSecurityOptionLen = sizeof(ulngSecurityOptions);
bReturn = WinHttpQueryOption(hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
&ulngSecurityOptions,
&ulngSecurityOptionLen);
if (bReturn)
{
ulngSecurityOptions |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
SECURITY_FLAG_IGNORE_UNKNOWN_CA;
bReturn = WinHttpSetOption(hRequest,
WINHTTP_OPTION_SECURITY_FLAGS,
&ulngSecurityOptions,
ulngSecurityOptionLen);
}
std::wstring strRequestHeader;
std::wstring strRequestContent;
strRequestHeader = L"https://cucm11-5/ccmadmin";
strRequestContent = L"";
bResults = WinHttpSendRequest( hRequest,
strRequestHeader.c_str(),
(unsigned long)strRequestHeader.length(),
(LPVOID)strRequestContent.c_str(),
(unsigned long)strRequestHeader.length(),
(unsigned long)strRequestHeader.length()+
(unsigned long)strRequestContent.length(),
0);
if (bResults)
bResults = WinHttpReceiveResponse( hRequest, NULL);
unsigned long ulngStatus = 0;
unsigned long ulngStatusSize = sizeof(ulngStatus);
if (bResults) {
bResults = WinHttpQueryHeaders(hRequest,
WINHTTP_QUERY_FLAG_NUMBER | WINHTTP_QUERY_STATUS_CODE,
0,
&ulngStatus,
&ulngStatusSize,
NULL);
}
printf( "Error Status Code - %d and last error \n", ulngStatus);
if (bResults)
{
do
{
dwSize = 0;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
{
printf( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
break;
}
if (!dwSize)
break;
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
break;
}
ZeroMemory(pszOutBuffer, dwSize+1);
if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
{
printf( "Error %u in WinHttpReadData.\n", GetLastError());
}
else
{
printf("%s", pszOutBuffer);
}
delete [] pszOutBuffer;
if (!dwDownloaded)
break;
} while (dwSize > 0);
}
else
{
printf( "Error %d has occurred.\n", GetLastError() );
}
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
getchar();
return 0;
}
What I have tried:
Am trying to connect "https://cucm11-5/ccmadmin"
The response received is 12002 but actually URL should return 200 which works with postman and browser.
Am new to winhttp, is that am missing any ?
i tried the following but still not resolved issue
- increasing set timeout values
- tried with IP instead of hostname