首页 > 解决方案 > 在 WinHttpReceiveResponse 接收 12030

问题描述

我刚刚开始使用带有 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是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"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 上。

标签: sslwinhttpwinhttprequest

解决方案


您在调用 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);

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


推荐阅读