sslwinhttpwinhttprequest

receiving 12030 at WinHttpReceiveResponse


I just got into using SSL with winhttp. I am receiving ERROR_WINHTTPCONNECTION_ERROR

in MSDN the docs it says,

The connection with the server has been reset or terminated, or an incompatible SSL protocol was encountered. For example, WinHTTP version 5.1 does not support SSL2 unless the client specifically enables it.

What could be the reason ? I tried different links.

gcc myFile.c -o myFile.exe -lwinhttp is the arg.

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <WinHttp.h>


#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS_1_1
# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200
#endif

#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS_1_2
# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
#endif

//  Variables
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL  bResults = FALSE;
HINTERNET  hSession = NULL,
           hConnect = NULL,
           hRequest = NULL;
int main(){
//  char vFileContent[][];

  // 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"learn.microsoft.com",
      //hConnect = WinHttpConnect( hSession, L"steamcdn-a.akamaihd.net",
                                 INTERNET_DEFAULT_HTTPS_PORT, 0);


  // Create an HTTP request handle.

  hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/en-us/windows/win32/debug/system-error-codes--1000-1299-",
  //    hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/client/installer/SteamSetup.exe",
                                     NULL, WINHTTP_NO_REFERER,  WINHTTP_DEFAULT_ACCEPT_TYPES, NULL);

  if (!hConnect)
    printf("Error %d has occurred at WinHttpOpenRequest2.\n",GetLastError());


  // Send a request.
  if (hRequest)
      bResults = WinHttpSendRequest( hRequest,
                                     WINHTTP_NO_ADDITIONAL_HEADERS,
                                     0, WINHTTP_NO_REQUEST_DATA, 0,
                                     0, 0);

  printf("Error %d has occurred at WinHttpSendRequest.\n",GetLastError());

  // End the request.



  if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);
  printf("Error %d has occurred at WinHttpReceiveResponse.\n",GetLastError());

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

          // Allocate space for the buffer.
          pszOutBuffer = (char *) malloc(dwSize+1);
        //  pszOutBuffer = new char[dwSize+1];
          if (!pszOutBuffer)
          {
              printf("Out of memory\n");
              dwSize=0;
          }
          else
          {
              // 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);
                              // Data in vFileContent
                //  vFileContent.push_back(pszOutBuffer);
              }

              // Free the memory allocated to the buffer.
              memset(pszOutBuffer, '\0', sizeof(pszOutBuffer));
              //delete [] pszOutBuffer;
          }

      } while (dwSize>0);


  // Report any errors.
  if (!bResults)
      printf("Error %d has occurred.\n",GetLastError());

  // Close any open handles.
  if (hRequest) WinHttpCloseHandle(hRequest);
  if (hConnect) WinHttpCloseHandle(hConnect);
  if (hSession) WinHttpCloseHandle(hSession);

}

Do I need an extra file or something ? I am on windows 10.


Solution

  • You are missing the WINHTTP_FLAG_SECURE flag from your call to WinHttpOpenRequest. That flag is required for HTTPS.

    Instead of this:

        hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/en-us/windows/win32/debug/system-error-codes--1000-1299-",
            NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, NULL);
    

    This:

        hRequest = WinHttpOpenRequest(hConnect, L"GET", L"/en-us/windows/win32/debug/system-error-codes--1000-1299-",
            NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
    

    When I added that flag, your program appears to work.