Click here to Skip to main content
14,980,370 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;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL, 
               hConnect = NULL,
               hRequest = NULL;

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

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

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

    // 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 |

          bResults = WinHttpSetOption(hRequest,

         /* WinHttpSetOption ( hRequest,
        std::wstring strRequestHeader;
        strRequestHeader = L"";
        bResults = WinHttpSendRequest( hRequest,
                                        (unsigned long)strRequestHeader.length(),
                                        WINHTTP_NO_REQUEST_DATA, 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,

        printf( "Error Status Code - %d and last error \n", ulngStatus);
    // Keep checking for data until there is nothing left.
    if (bResults)
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable( hRequest, &dwSize)) 
                printf( "Error %u in WinHttpQueryDataAvailable.\n",
            // No more available data.
            if (!dwSize)

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

            if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                  dwSize, &dwDownloaded))
                printf( "Error %u in WinHttpReadData.\n", GetLastError());
                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)
        } while (dwSize > 0);
        // 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);

    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 "" where reponse is sucess but opening in browser/postman gives 404 error.

Please what need to done to get response 404?
Updated 24-May-21 22:24pm
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,

printf( "Error Status Code - %d and last error \n", ulngStatus);

status says 200 instead of 404,

The status code it returned by quering "" instead of full "".. 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.
Member 11732139 25-May-21 5:24am
am very new to winhttp.
i tried writing some code based on reference with my project.

The below code
bResults = WinHttpQueryHeaders(hRequest,

printf( "Error Status Code - %d and last error \n", ulngStatus);

status says 200 instead of 404

Stefan_Lang 25-May-21 6:34am
Well, that is no new information. But the things I pointed out are not related to winhttp at all, but to the structure of your code in general: to arrive at the desired results, you need to perform a serious of function calls, and you can only expect your program to work as expected if _all_ of those calls succeed. Consequently, if any of your calls fail, you should stop.

The problem is that probably something failed before you call WinHttpQueryHeaders, and therefore this function cannot succeed! Check all result vvalues and print out an error message if any of these calls fail. Then you'll see what's going wrong.

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