WinHTTP错误12029无法连接

时间:2013-07-13 00:08:51

标签: c++ windows connection winhttp

我正在尝试与我自己的域建立http连接并返回一个字符串。我有它与www.microsoft.com合作,但当我将域名更改为www.atomic-gaming.info(我的域名)时,我收到错误12029,如果我将链接更改为www.yahoo.com,我会收到错误12017

 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 );
 WinHttpSetOption(hSession, WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS,NULL,NULL);
 // 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 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 );

有人可以告诉我为什么这只适用于microsoft.com吗?我将如何解决它?

1 个答案:

答案 0 :(得分:0)

您正在通过HTTPS进行连接。 www.atomic-gaming.info没有提供有效的SSL证书,因此出于安全原因,WinHTTP拒绝连接到它(这可以被覆盖)。

www.yahoo.com重定向到HTTP。它不适合您,因为您错误地调用了WinHttpSetOption。做到这一点

DWORD opt = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS;
WinHttpSetOption(hSession, WINHTTP_OPTION_REDIRECT_POLICY, &opt, sizeof(opt));