private void OnStreamRead(IAsyncResult result)
{
RequestState state = result.AsyncState as RequestState;
// Retrieve the ResponseStream that was set in RespCallback.
Stream responseStream = state.ResponseStream;
// Read rs.BufferRead to verify that it contains data.
int read = responseStream.EndRead(result);
if (read > 0)
{
// Prepare a Char array buffer for converting to Unicode.
Char[] charBuffer = new Char[RequestState.BUFFER_SIZE * 2];
// Convert byte stream to Char array and then to String.
// len contains the number of characters converted to Unicode.
int len =
state.StreamDecode.GetChars(state.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);
// Append the recently read data to the RequestData stringbuilder
// object contained in RequestState.
state.RequestData.Append(str);
// Continue reading data until
// responseStream.EndRead returns –1.
IAsyncResult ar = responseStream.BeginRead(
state.BufferRead, 0, RequestState.BUFFER_SIZE,
new AsyncCallback(OnStreamRead), state);
}
else
{
if (state.RequestData.Length > 0)
{
DownloadedPage = new Page(state.RequestData.ToString(), Uri);
}
Completed = true;
// Close down the response stream.
responseStream.Close();
}
}