Hi TL.
Try this sample code. Replace string tokenizer with L"Your Text:". Most
of the code is from MSDN. Check WinHttpQueryHeaders in MSDN .
// 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"
Post by TLHi, I have a Program wiich works fine from Win2000Pro up to XP SP1.
Afte installing SP2 on my XP-Systems, the call to HTTPOpenRequest fails with
"A Databuffer given to a Systemcall is too small"
hRequest := HttpOpenRequest(hConnect, 'POST', URLC.lpszUrlPath, NIL, NIL,
@Accept, 0, 1);
Has anybody a Idea ?