1

hi i am implementing post json request with winhttp function. though no error in code but at time of execution i cant get proper output for it. it gives me error for it like Error 87 has occurred. Could not close the hSession handle. it means open and is not able to be close please give me suggestions how should i remove that error. here is my code

#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>


int main(int argc, char* 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"WinHTTP Example/1.0",  
                            WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                            WINHTTP_NO_PROXY_NAME, 
                            WINHTTP_NO_PROXY_BYPASS, 0);

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

  //   Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"POST", L":8080/hellowword.jsp",
                                       NULL, WINHTTP_NO_REFERER, 
                                       WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                       WINHTTP_FLAG_SECURE);

TCHAR* szHeaders =  _T("Content-Type:application/json\r\n");
TCHAR* szPostData = _T("{\"command\":\"remotecontrol\",\"method\":\"countmon.getgatestatus\",\"param\":\"2\"}");



    if (hRequest)
        bResults = WinHttpSendRequest( hRequest,
                                       szHeaders, _tcslen(szHeaders),  
                                      szPostData, _tcslen(szPostData) 
                                       ,0, 0);


 //    End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);

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

    system("pause");
    return 0;

    }
user3505712
  • 895
  • 1
  • 11
  • 20

0 Answers0