Download image from server using WINHTTP functions

  • Thread starter Thread starter Abhay990
  • Start date Start date
A

Abhay990

Guest
Hi,

I am trying to download image file from server but its not working.

It requires secure communication

Getting system error 12152 - Unable to parse system response

I am able to receive all text files fine, but not image and video too.

Please suggest

Below is ref to my code

// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"CWebFile HTTP Transfer Class/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );

// Specify an HTTP server.
if ( hSession != NULL )
{
// Data is read in small chucks, so timeouts longer than the default are not required.
bResults = WinHttpSetTimeouts( hSession, nDNS_RESOLUTION_TIMEOUT, nCONNECT_TIMEOUT, nSendTimeout, nReceiveTimeout );

if ( m_bSecureTransfer == true )
{
dwProxyAuthScheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
hConnect = WinHttpConnect( hSession, m_sDomain,
INTERNET_DEFAULT_HTTPS_PORT, 0);
}
else
{
dwProxyAuthScheme = 0;
hConnect = WinHttpConnect( hSession, m_sDomain,
usPort, 0);
}
}

// Create an HTTP Request handle.
if ( hConnect != NULL )
{
sFileName = m_sFileName;

// Ensure all paramaters are filled in for a secure file transfer
CheckSecureTransferMode();

if ( m_bSecureTransfer == true )
{
const wchar_t *att[] = { L"text/*", L"image/*", L"audio/*", L"video/*", NULL };
hRequest = WinHttpOpenRequest( hConnect, L"GET", sFileName, NULL, WINHTTP_NO_REFERER,
att/*WINHTTP_DEFAULT_ACCEPT_TYPES*/, WINHTTP_FLAG_SECURE );

DWORD dwTmp = SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_UNKNOWN_CA;
WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, (LPVOID)&dwTmp, sizeof(dwTmp));

dwTmp = WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS;
//WinHttpSetOption(hRequest, WINHTTP_OPTION_REDIRECT_POLICY, (LPVOID)&dwTmp, sizeof(dwTmp));
}
else
{
hRequest = WinHttpOpenRequest( hConnect, L"GET", sFileName, NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, 0 );
}
}

while ( bDone == FALSE )
{
if ( dwProxyAuthScheme != 0 )
{
bResults = WinHttpSetCredentials( hRequest, WINHTTP_AUTH_TARGET_SERVER,
dwProxyAuthScheme,
m_sUserName,
m_sPassword,
NULL );
}

bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);

// End the request.
if ( bResults == TRUE )
{
bResults = WinHttpReceiveResponse( hRequest, NULL );
}

if ( bResults == TRUE )
{
// Make sure that the file was actually opened OK and we did not get an HTTP
// error page.
bResults = WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
NULL, &dwStatusCode, &dwSize, NULL );

if ( bResults == TRUE )
{
if ( dwStatusCode != HTTP_STATUS_OK )
{
bResults = FALSE;
}
}
}

// Read data from the server.
if ( bResults == TRUE )
{
do
{
dwAvailableBytesToRead = 0;
bResults = WinHttpQueryDataAvailable( hRequest, &dwAvailableBytesToRead );

dwAvailableBytesToRead = min( dwMaxSize, dwAvailableBytesToRead );
if ( bResults == TRUE )
{
baFileChunk.SetSize( dwAvailableBytesToRead );
bResults = WinHttpReadData( hRequest, baFileChunk.GetData(), (DWORD)baFileChunk.GetSize(),
&dwBytesRead );
baFile.Append( baFileChunk );
}
else
{
sTempMessage.Format( "Error in Receiving file name (%s) on HTTP, Investigate: AvailableBytesToRead: %d, Error: %d: .\n", m_sFileName, dwAvailableBytesToRead, GetLastError() );
m_lseLimitedLog.LogSystemEvent( sTempMessage, ESystemEventLogSubSystem_WiTalk, ESystemEventLogAlertLevel_Debug );
}

} while ( ( (DWORD)baFile.GetSize() < dwMaxSize ) &&( dwAvailableBytesToRead > 0 ) && ( bResults == TRUE ) );
}

// Get last error
dwLastError = GetLastError();

Continue reading...
 
Back
Top