Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get Content-Length in a download?

73 views
Skip to first unread message

ms

unread,
Feb 18, 2005, 6:32:22 AM2/18/05
to
Hi,

Im using HTTP functions and want to know how to parse the headers returned
by ther server so i can get the value of Content-Length. I can't find the
way since, the functions seems only to support data returned but not the
response headers.

Please help,
Vikas


Rob Grainger

unread,
Feb 28, 2005, 6:41:04 PM2/28/05
to
Vikas,

Try
http://msdn.microsoft.com/library/en-us/wininet/wininet/retrieving_http_headers.asp?frame=true

(thats one URL split over two lines (in my browser anyway), but it should
answer your question.

Rob

Murugan Andezuthu Dharmaratnam

unread,
Apr 15, 2005, 2:39:08 AM4/15/05
to
Hi Vikas,
Attaching code snippet for WinHTTP. Similar to WinInet , I got code for
WinInet Also , They are almost the same. This is a code for checking if
download by part is supported. Additional HTTP header is added. Major
Portion of code is from MSDN. Replace StringTokenizer with Unicode Text
L"Text".


// HTTPManager.cpp: implementation of the HTTPManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "HTTPManager.h"
#include "StringTokenizer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

HTTPManager::HTTPManager()
{

}

HTTPManager::~HTTPManager()
{

}

bool HTTPManager::HTTPGetFileInfo(LPTSTR URL)
{

// ANSI TO Unicdoe


DWORD dwSize = 0;
LPVOID lpOutBuffer = NULL;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;

StringTokenizer* URLStringTokenizer = new StringTokenizer();
URLStringTokenizer->SetStringToken(URL,"/");
printf("%s",URLStringTokenizer->NextToken());

char szTemp[100] = "000000000000000000000000000000000000000000";
strcpy(szTemp ,URLStringTokenizer->NextToken("/"));
WCHAR wszTemp[100+1]; // Unicode ServerName
MultiByteToWideChar( CP_ACP, 0, szTemp ,
strlen(szTemp )+1, wszTemp,
100);
printf("\n%s",szTemp);


// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"DA 7.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );


// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession,wszTemp,
INTERNET_DEFAULT_PORT, 0 );


strcpy(szTemp ,URLStringTokenizer->NextToken(" "));
if(szTemp[strlen(szTemp)-2] == '\r')
szTemp[strlen(szTemp)-2] = '\0';
printf("\n%s",szTemp);

MultiByteToWideChar( CP_ACP, 0, szTemp ,
strlen(szTemp )+1, wszTemp,
100);

// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", wszTemp,
NULL, WINHTTP_NO_REFERER,
0,
0);
// Add a request header.
if( hRequest )
bResults = WinHttpAddRequestHeaders( hRequest,
L"Range: bytes=0-",
-1,
WINHTTP_ADDREQ_FLAG_ADD );


// 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 );


/* New Added */


// First, use HttpQueryInfo to obtain the size of the buffer.
if (bResults)
{
WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize,
WINHTTP_NO_HEADER_INDEX);

// Allocate memory for the buffer.
if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )
{
lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];

// Now, use HttpQueryInfo to retrieve the header.
bResults = WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_RAW_HEADERS_CRLF,
WINHTTP_HEADER_NAME_BY_INDEX,
lpOutBuffer, &dwSize,
WINHTTP_NO_HEADER_INDEX);
}
}

// Print the header contents.
if (bResults)
printf("Header contents: \n%S",lpOutBuffer);

/*End New Added - Delete Comment Final */

// 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( ) );
printf("\n Number Of Bytes - %d \n",dwSize);
// 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 );


return false;
}


Hope this helps. If Needed I will write a sample projegram and send it to u.
I will post the StringTokenizer class in my web site on Apr 18 Monday (Need
to add few lines to comments :) ).


Thanks!
Murugan
www.muruganad.net

"Nothing is Impossible, Its just Improbable"


"Rob Grainger" <RobGr...@discussions.microsoft.com> wrote in message
news:37EF599F-784A-4F43...@microsoft.com...

0 new messages