我试图确认这里描述的功能:
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 个 IP 地址中的每一个。如果在指定的尝试次数后连接尝试仍然失败,或者在此之前连接超时过期,则取消请求。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,但事实并非如此。我错过了什么?