从websource的字节数下载 [英] Download number of bytes from websource
问题描述
我想下载一些使用的HttpWebRequest
从websource字节(可以是任何不同的方式 - 我试过的WebRequest,HttpClient的...)在Windows手机8.1运行时 - 完整的代码:
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");
}
问题 - 代码工作,但OS是某种防止读取字节的少量(甚至我已经停用 AllowReadStreamBuffering
) - (?)它似乎整个文件下载到一些缓存,然后运行 while循环
。它看起来是这样的:
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:
正如你可以看到 - 个字节的第一个量22秒之后出现 - 文件下载整。相反,当我bulid在Windows Phone 8.1 Silverligh相同的代码(复制粘贴),并且在同一台设备上运行 - 它运行,因为它应该:
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:
有没有下载的WP8字节数的方法。 1运行时,无需先下载整个文件
Is there any method to download number of bytes on WP8.1 Runtime, without downloading the whole file first?
推荐答案
下面是不得已而为之的想法 - 用的插座! 。这是非常酷的,我知道
Here is a last resort idea - use sockets! It's very uncool, I know.
如果你需要的是不是超级复杂,您可以使用这两种东西混合起来:的 NET HTTP套接字库并的 TcpClient的WP为。我最近需要这对于一款Windows Phone 8.0的应用程序,所以我使用这些库,这是工作的罚款
If what you need is not super complex, you can mix something up using these two: .NET Http socket library and TcpClient for WP. I recently needed this for a Windows Phone 8.0 app, so I used these libraries and it is working fine.
编辑 - 演示项目
下面是一个示范项目,做什么,我只是建议。这没什么史诗,但似乎这样的伎俩。如果你不需要的东西太复杂,它应该正常工作
Here's a demo project that does what I just suggested. It's nothing epic, but it seems to do the trick. If you don't need something too complex, it should work fine.
Offtopic - 为什么我需要插座
最近,我曾与HTTP请求的所有其他可用选项的一个问题 - 他们都需要在UI线程可以自由为了工作(至少在Windows手机,不知道WinRT的)。那就是 - 他们实际上做一些事情,即使你从另一个线程使用它们的UI线程。这一般是罚款......除了在我的处境我真的以阻止UI线程需要的。
Recently I had an issue with all other available options for http requests - they all need the UI thread to be free in order to work (at least in Windows Phone, not sure about WinRT). That is - they actually do something in the UI thread even if you use them from another thread. And that is generally fine... except in my situation I really needed to block the UI thread.
这篇关于从websource的字节数下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!