I'm trying to download a number of bytes from websource using HttpWebRequest
(can be any different way - I've tried WebRequest, HttpClient ...) on Windows Phone 8.1 Runtime - complete code:
private async void Download1000_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Download Started");
HttpWebRequest longRequest = (HttpWebRequest)WebRequest.Create(new Uri(@"http://s3.amazonaws.com/dnr/dotnetrocks_0986_enterprise_sharepoint.mp3"));
longRequest.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString(); // prevent caching the whole file
longRequest.AllowReadStreamBuffering = false;
using (WebResponse myResponse = await longRequest.GetResponseAsync())
using (Stream myStream = myResponse.GetResponseStream())
{
int bytesRead = 0;
byte[] myBuffer = new byte[1000];
Stopwatch newWatch = new Stopwatch();
newWatch.Start();
while ((bytesRead = await myStream.ReadAsync(myBuffer, 0, 1000)) > 0)
Debug.WriteLine(bytesRead.ToString() + " bytes read. Elapsed time: " + newWatch.Elapsed.TotalSeconds.ToString("0.000000") + " seconds");
}
Debug.WriteLine("Download Finished");
}
The problem - the code is working, but OS is somehow preventing reading small amount of bytes (even I've disabled AllowReadStreamBuffering
) - it seems that it downloads the whole file to some cache (?) and then runs the while loop
. It looks like this:
As you can see - the first amount of bytes appears after 22 seconds - the file was downloaded whole. Contrary when I bulid the same code (copy-paste) on Windows Phone 8.1 Silverligh, and run on the same device - it runs as it should:
Is there any method to download number of bytes on WP8.1 Runtime, without downloading the whole file first?