当主机名不包含 www 时,WinHttp 不起作用。(错误 12029)
WinHttp doesn't work when hostname doesn't contains www. (error 12029)
我正在测试这个来自 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_*
选项。
相关文章:
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 键盘不起作用 - Arduino Uno - 与变量有关的多个错误
- 为什么int myVar{5}给出错误并且不起作用?
- 类和构造函数中的函数根本不起作用,并且不返回任何错误
- 请发现它在 dev 中工作的错误,但在 hackerrank 中不起作用
- 函数错误 C2059:语法错误:'>'不起作用
- C ++函数重新定义(代码不起作用 - 逻辑错误)
- SFML 加载从文件不起作用,奇怪的错误
- 函数模板不起作用,出现错误"No suitable user-defined conversion"
- C++插入排序错误功能不起作用
- 任务计划程序库的预处理不起作用 - 多定义错误
- 朋友功能不起作用(语法错误)
- 模板化友元声明在 G++ 5.4.0 下不起作用 - 编译器错误或错误代码
- 我已经在 C++ 中实现了合并排序。它没有错误,但不知何故不起作用
- GCC 显示系统库中的错误.-iSystem 不起作用
- C++ 包含防护不起作用,错误
- 为什么此C 代码不起作用 - 试图删除集合中的元素时,请参考错误
- 获得程序输出(无错误),但程序.exe在使用 graphics.h 时不起作用
- Wextra的Qt创建器错误不起作用
- 枚举返回类型不起作用 C [错误 C2440]