在WinHttpReceiveResponse处接收12030

问题描述 投票:0回答:1

我刚开始在winhttp中使用SSL。我收到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上。

ssl winhttp winhttprequest
1个回答
0
投票

您在WinHttpOpenRequest的调用中缺少WINHTTP_FLAG_SECURE标志。 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);

当我添加该标志时,您的程序似乎可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.