我正在尝试HttpWebRequest
在 Windows Phone 8.1 运行时使用(可以是任何不同的方式 - 我尝试过 WebRequest、HttpClient ...)从 websource 下载一些字节 - 完整代码:
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");
}
问题- 代码正在运行,但操作系统以某种方式阻止读取少量字节(即使我已经禁用AllowReadStreamBuffering
) - 似乎它将整个文件下载到某个缓存(?),然后运行while loop
. 它看起来像这样:
如您所见 - 第一个字节数在 22 秒后出现 - 文件被整个下载。相反,当我在 Windows Phone 8.1 Silverligh 上构建相同的代码(复制粘贴)并在同一设备上运行时 - 它按应有的方式运行:
有什么方法可以在 WP8.1 Runtime 上下载字节数,而无需先下载整个文件?