Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
// 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;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL, 
               hConnect = NULL,
               hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen( L"NetIQ AppManager",  
                            WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                            L"cucm-11", 
                            NULL, 0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect( hSession, L"10.71.112.251",
                                   INTERNET_DEFAULT_HTTPS_PORT, 0);

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
                                       NULL, WINHTTP_NO_REFERER, 
                                       WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                       WINHTTP_FLAG_SECURE);

    // Send a request.
    if (hRequest)
    {
        unsigned long ulngSecurityOptions = 0;
        unsigned long ulngSecurityOptionLen = sizeof(ulngSecurityOptions);

        ulngSecurityOptions |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
                                   SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
                                   SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
                                   SECURITY_FLAG_IGNORE_UNKNOWN_CA;

          bResults = WinHttpSetOption(hRequest,
                                       WINHTTP_OPTION_SECURITY_FLAGS,
                                       &ulngSecurityOptions,
                                       ulngSecurityOptionLen);

         /* WinHttpSetOption ( hRequest,
                               WINHTTP_OPTION_CLIENT_CERT_CONTEXT,
                               WINHTTP_NO_CLIENT_CERT_CONTEXT,
                               0);*/
        std::wstring strRequestHeader;
        strRequestHeader = L"https://10.71.112.251//appadmin";
        bResults = WinHttpSendRequest( hRequest,
                                        strRequestHeader.c_str(),
                                        (unsigned long)strRequestHeader.length(),
                                        NULL, 
                                        WINHTTP_NO_REQUEST_DATA, 0, 
                                       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:

i tried with different winhttp setoption.
it queries the default page and returns sucess(which is correct)
as per my changes it has query "https://10.71.112.251//appadmin" where reponse is sucess but opening in browser/postman gives 404 error.

Please what need to done to get response 404?
Posted
Updated 24-May-21 22:24pm
v2
Comments
Richard Deeming 25-May-21 4:27am    
Are you absolutely sure that the URL returns a 404 status code in postman, and not a 404 error page with a status code of 200?
Member 11732139 25-May-21 4:39am    
yes browser shows 404 error but winhttp response says 200

Richard Deeming 25-May-21 4:41am    
The browser isn't going to show you the status code at all, unless you look at a network trace in the developer tools.

As I said, if your winhttp code is returning a status code of 200, it's likely that the site is returning a "page not found" error page with an incorrect status code. That's not something you can fix; it's something the people running the site need to fix.
Member 11732139 25-May-21 5:27am    
The below code
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);


status says 200 instead of 404,

The status code it returned by quering "https://10.71.112.251" instead of full "https://10.71.112.251//appadmin".. is that the issue ?then how to query full url ?
Stefan_Lang 25-May-21 5:06am    
"Please what need to done to get response 404? "
First of all, format your code properly. A plain text dump is not something people are likely to read on their free time!

That said, it looks like you only have one long main function. That's the first problem right there: too much complexity within one function makes it a lot harder to understand what it's supposed to do, and what it really does.

Moreover, I see #include <iostream>, but no use of cin or cout, or any other feature of the C++ language - is your code supposed to be C++ or C? You might want to consider introducing classes for sessions, connections and requests and then use RAII to clean up dependencies and error handling: that would be the sensible thing to do, and it would help reducing complexity.

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