Reputation: 262
I'm trying to download data from a webpage then parse it, the problem is that I cant obtain the value of pszoutbuffer
(ZeroMemory
function deletes it) i took the code from MSDN example
void http_connect::read_data(void)
{
// 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());
}
}
} while( dwSize > 0 );
}
}
Upvotes: 0
Views: 1527
Reputation: 182753
This example code just doesn't do anything with the data. You should add code to do something with the data, like this:
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
{
// add code here to do something with the data
// data is in pszOutBuffer and byte count is dwSize
}
}
} while( dwSize > 0 );
The call to ZeroMemory
is not needed. It's probably there to prevent you from getting confused if your program erroneously tries to access the data past the point that was read. This is not needed if your code follows basic sanity rules for dealing with data received over the network -- do not read past dwSize
and do not assume the data has a terminating zero byte.
Upvotes: 0
Reputation: 1675
I think that immediately after WinHttpReadData you should append the contents of pszOutBuffer into a string so that you can use it later on.
As you said, the pszOutBuffer keeps getting over-written on each loop iteration. It looks like it's just temporary storage for you to copy into something permanent.
Upvotes: 2