当第一个 ip 错误时使用 winhttp 连接到多宿主主机
我试图确认此处描述的功能:
http:// /msdn.microsoft.com/en-us/library/aa384066(VS.85).aspx
WINHTTP_OPTION_CONNECT_RETRIES 设置或 检索无符号长整型 包含数量的值 WinHTTP 尝试连接到的次数 主持人。 Microsoft Windows HTTP 服务 (WinHTTP) 每个仅尝试一次 互联网协议 (IP) 地址。为了 例如,如果您尝试连接到 具有 10 个 IP 的多宿主主机 地址和 设置 WINHTTP_OPTION_CONNECT_RETRIES 到 7,那么 WinHTTP 仅尝试 连接到前七个 IP 地址。 给定同一组 10 个 IP 地址, 如果 WINHTTP_OPTION_CONNECT_RETRIES 是 设置为 20,WinHTTP 尝试每个 10只一次。如果连接 之后尝试仍然失败 指定的尝试次数,或者如果 连接超时之前已过期 然后,请求被取消。这 默认值 WINHTTP_OPTION_CONNECT_RETRIES 为 5 尝试。
实际上有效。
我的代码是:
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,但事实并非如此。我缺少什么?
im trying to confirm that the feature described here:
http://msdn.microsoft.com/en-us/library/aa384066(VS.85).aspx
WINHTTP_OPTION_CONNECT_RETRIES Sets or
retrieves an unsigned long integer
value that contains the number of
times WinHTTP attempts to connect to a
host. Microsoft Windows HTTP Services
(WinHTTP) only attempts once per
Internet Protocol (IP) address. For
example, if you attempt to connect to
a multihomed host that has 10 IP
addresses and
WINHTTP_OPTION_CONNECT_RETRIES is set
to 7, then WinHTTP only attempts to
connect to the first seven IP address.
Given the same set of 10 IP addresses,
if WINHTTP_OPTION_CONNECT_RETRIES is
set to 20, WinHTTP attempts each of
the 10 only once. If a connection
attempt still fails after the
specified number of attempts, or if
the connect timeout expired before
then, the request is canceled. The
default value for
WINHTTP_OPTION_CONNECT_RETRIES is five
attempts.
actually works.
my code is:
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;
}
one way to test this is to place this in your C:\WINDOWS\system32\drivers\etc\hosts file:
123.123.123.123 google.com
66.102.7.99 google.com
type
ping google.com
(this should fail)
then
ipconfig /displaydns
and you should see two dns records for google.com.
as stated above, the default value is 5 which means it should go on to try the second ip, but it isn't. what am i missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论