Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

c# - Download number of bytes from websource

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:

withSomeCache

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:

withoutCache

Is there any method to download number of bytes on WP8.1 Runtime, without downloading the whole file first?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a last resort idea - use sockets! It's very uncool, I know.

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.

Edit - Demo project

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 - why I needed sockets

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...