当第一个ip坏时,使用winhttp连接到多宿主主机

时间:2010-08-04 17:41:07

标签: c++ winhttp multihomed

我试图确认此处描述的功能:

http://msdn.microsoft.com/en-us/library/aa384066(VS.85).aspx

  

WINHTTP_OPTION_CONNECT_RETRIES设置或   检索无符号长整数   包含数量的值   WinHTTP试图连接到一个   主办。 Microsoft Windows HTTP服务   (WinHTTP)只尝试一次   Internet协议(IP)地址。对于   例如,如果您尝试连接到   一个拥有10个IP的多宿主主机   地址和   WINHTTP_OPTION_CONNECT_RETRIES已设置   到7,然后WinHTTP只尝试   连接到前七个IP地址。   给定相同的10个IP地址,   如果WINHTTP_OPTION_CONNECT_RETRIES是   设置为20,WinHTTP尝试每个   10只一次。如果是连接   尝试后仍然失败   指定的尝试次数,或者是否   连接超时之前已过期   然后,请求被取消。该   默认值   WINHTTP_OPTION_CONNECT_RETRIES是五   尝试。

实际上有效。

我的代码是:

int main()
{   
  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 );
    DWORD data;
    DWORD dwSize = sizeof(DWORD);
    if (hSession)
    {


        // Use WinHttpQueryOption to retrieve internet options.
        if (WinHttpQueryOption( hSession, 
                                WINHTTP_OPTION_CONNECT_RETRIES, 
                                &data, &dwSize))
        {
            printf("Connection Retries: %u \n\n",data);
        }
        else
        {
            printf( "Error %u in WinHttpQueryOption.\n", GetLastError());
        }        
    }
    else
    {
        printf("Error %u in WinHttpOpen.\n", GetLastError());
    }

dwSize = 0;

   // Specify an HTTP server.
  if( hSession )
    hConnect = WinHttpConnect( hSession, L"google.com",
                               INTERNET_DEFAULT_HTTP_PORT, 0 );

  // Create an HTTP request handle.
  if( hConnect )
    hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
                                   NULL, WINHTTP_NO_REFERER, 
                                   NULL, 
                                   NULL );

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

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

    getchar();

 return 0;
}

测试此方法的一种方法是将其放在C:\ WINDOWS \ system32 \ drivers \ etc \ hosts文件中:

123.123.123.123 google.com

66.102.7.99 google.com

ping google.com

(这应该失败) 那么

ipconfig /displaydns

你应该看到google.com的两个dns记录。

如上所述,默认值为5,这意味着它应该继续尝试第二个ip,但事实并非如此。我错过了什么?

0 个答案:

没有答案