получая 12030 на WinHttpReceiveResponse - PullRequest
0 голосов
/ 14 января 2020

Я только что начал использовать SSL с winhttp. Я получаю ERROR_WINHTTPCONNECTION_ERROR

в MSDN документы, на которых написано:

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.

В чем может быть причина? Я пробовал разные ссылки.

gcc myFile.c -o myFile.exe -lwinhttp это аргумент.

#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"docs.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);

}

Нужен ли мне дополнительный файл или что-то? Я на windows 10.

1 Ответ

1 голос
/ 14 января 2020

Вы пропускаете флаг WINHTTP_FLAG_SECURE при вызове WinHttpOpenRequest. Этот флаг необходим для HTTPS.

Вместо этого:

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

Это:

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

Когда я добавил этот флаг, ваша программа, кажется, работает.

...