-1

我正在尝试与我自己的域建立 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吗?我将如何修复它?

4

1 回答 1

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));
于 2013-07-13T02:20:14.493 回答