Reputation: 507
How is it possible to read the received data from a HttpWebRequest asynchronously while the request is downloading the resource?
I want to download a file and immediately start processing the received bytes. I've found a lot of examples how to call the HttpWebRequest asynchronously but the data gets read not until the download has completed:
HttpWebRequest webRequest;
void StartWebRequest()
{
webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}
void FinishWebRequest(IAsyncResult result)
{
webRequest.EndGetResponse(result);
}
Upvotes: 0
Views: 115
Reputation: 120380
At the line
webRequest.EndGetResponse(result);
you still haven't read any of the response body. EndGetResponse returns an HttpWebResponse on which you must call GetResponseStream. The stream contains the actual response body and you can read this as fast or as slow as you want using standard stream methods.
Upvotes: 2