当主机名不包含 www 时,WinHttp 不起作用。(错误 12029)

WinHttp doesn't work when hostname doesn't contains www. (error 12029)

本文关键字:不起作用 错误 12029 WinHttp 主机 包含 www      更新时间:2023-10-16

我正在测试这个来自 http://msdn.microsoft.com/en-us/library/aa384270%28v=vs.85%29.aspx 的winhttp示例

  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"www.microsoft.com",
                               INTERNET_DEFAULT_HTTPS_PORT, 0 );
  // Create an HTTP request handle.
  if( hConnect )
    hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
                                   NULL, WINHTTP_NO_REFERER, 
                                   WINHTTP_DEFAULT_ACCEPT_TYPES, 
                                   WINHTTP_FLAG_SECURE );
  // Send a request.
  if( hRequest )
    bResults = WinHttpSendRequest( hRequest,
                                   WINHTTP_NO_ADDITIONAL_HEADERS, 0,
                                   WINHTTP_NO_REQUEST_DATA, 0, 
                                   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( ) );
      // Allocate space for the buffer.
      pszOutBuffer = new char[dwSize+1];
      if( !pszOutBuffer )
      {
        printf( "Out of memoryn" );
        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 );
        // Free the memory allocated to the buffer.
        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 );

这工作正常,但是如果我从 www.microsoft.com 中取出www.,则出现12029 ERROR_WINHTTP_CANNOT_CONNECT错误,这是什么原因?

如果使用 www.,所有网站都可以使用 winhttp 吗? 因为有些网站不使用 www,我现在找不到任何网站,但我看到一些网站在使用 www 访问时在浏览器上显示"找不到服务器"。 但他们成功了。

microsoft.com 返回一个301 Moved Permanently标头,要让 winhttp 遵守该标头并重定向到Location:标头(www.)中的 URL,您需要使用 WinHttpSetOption 来设置适当的WINHTTP_OPTION_REDIRECT_*选项。