Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


// winhttpcalltest.cpp : Defines the entry point for the console application.
//

#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;

    // Use WinHttpOpen to obtain a session handle.
	std::wstring strUserAgent;
	strUserAgent = L"NETIQ AppManger";
	hSession = WinHttpOpen( strUserAgent.c_str(),  
                            WINHTTP_ACCESS_TYPE_NO_PROXY,
							NULL, 
                            NULL, 0);

	//settime option
	BOOL bReturn = WinHttpSetTimeouts(hSession,     // HINTERNET hInternet,
                                          12000,   // int dwResolveTimeout
                                          12000,   // int dwConnectTimeout
                                          12000,   // int dwSendTimeout
                                          12000);  // int dwReceiveTimeout


    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect( hSession, L"cucm11-5",
                                   8443, 0);

    // Create an HTTP request handle.
    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);

 
    // End the request.
    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);
    // Keep checking for data until there is nothing left.
    if (bResults)
    {
        do 
        {
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) 
            {
                printf( "Error %u in WinHttpQueryDataAvailable.\n",
                        GetLastError());
                break;
            }
            
            // No more available data.
            if (!dwSize)
                break;

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize+1];
            if (!pszOutBuffer)
            {
                printf("Out of memory\n");
                break;
            }
            
            // Read the Data.
            ZeroMemory(pszOutBuffer, dwSize+1);

            if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                  dwSize, &dwDownloaded))
            {                                  
                printf( "Error %u in WinHttpReadData.\n", GetLastError());
            }
            else
            {
                printf("%s", pszOutBuffer);
            }
        
            // Free the memory allocated to the buffer.
            delete [] pszOutBuffer;

            // This condition should never be reached since WinHttpQueryDataAvailable
            // reported that there are bits to read.
            if (!dwDownloaded)
                break;
                
        } while (dwSize > 0);
    }
    else
    {
        // Report any errors.
        printf( "Error %d has occurred.\n", GetLastError() );
    }

    // Close any open handles.
    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
Posted
Updated 8-Jun-21 0:53am
Comments
Richard MacCutchan 8-Jun-21 6:42am    
You need to use the debugger to see exactly what is received from the remote site.

1 solution

I suspect you are getting confused between the server response code and the error code returned from GetLastError. System network error code 12002 is ERROR_INTERNET_TIMEOUT as described at Error Messages (Wininet.h) - Win32 apps | Microsoft Docs[^].
 
Share this answer
 
Comments
Member 11732139 8-Jun-21 11:21am    
sorry .. it was my mistake.
the return value from WinHttpReceiveResponse is false and The getlasterror after WinHttpReceiveResponse is 12002 which is ERROR_INTERNET_TIMEOUT.
what could be reason , is that am missing any in send request or open request ?

From Browser or postman , the URL works fine and says 200 which is sucessful..
Richard MacCutchan 8-Jun-21 11:26am    
There are many reasons why a request may time out, but the only way to find the answer is by debugging and testing.
Member 11732139 9-Jun-21 4:52am    
As am newbie to winhttp ,can you suggest any debug/tracing tool of winhttp?
I got information about WinHttpTraceCfg.exe but am not able to get downloadable file neither the win2003 resource content pack
Richard MacCutchan 9-Jun-21 5:13am    
Use the Visual Studio debugger. I do not know of any tools for network tracing, but I am sure google will know of some.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900