我正在尝试将文件下载到带有覆盆子pi单声道的usb磁盘上 .

我正在使用框架4.5和HttpClient类来完成这个任务,似乎一切都好,除非CPU增长到100% . 这个是正常的?或者我做错了什么?,如果我尝试使用curl下载相同的文件,则在下载文件时CPU会以5% - 10%的速度运行 .

这是我的代码示例:

using System;
using System.IO;
using System.Net.Http;

namespace StreamSpeedTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Sample video File
            const string fileUrl = "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi";
            // SaveTo location
            const string file = "/tmp/big_buck_bunny.avi";

            Console.WriteLine("Press any key to start ...");
            Console.ReadKey();

            var buffer = new byte[80 * 1024];

            var client=new HttpClient();
            var response = client.GetAsync(fileUrl, HttpCompletionOption.ResponseHeadersRead);
            if (response.Result.IsSuccessStatusCode)
            {
                var stream = response.Result.Content.ReadAsStreamAsync().Result;

                var finfo = new FileInfo(file);

                if (finfo.Directory == null)
                {
                    Console.WriteLine("Wrong file path!");
                    return;
                }

                if (!finfo.Directory.Exists) finfo.Directory.Create();

                Console.WriteLine("Downloading data ...");
                using (var wrtr = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None, buffer.Length))
                {
                    var read=0;
                    while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) 
                    {
                        wrtr.Write(buffer,0,read);
                    }
                    wrtr.Flush();
                    wrtr.Close();
                }

                Console.WriteLine("Data downloaded!");

                stream.Close();
            }
            else
            {
                Console.WriteLine("Response Failed");
            }
        }
    }
}

我还尝试使用async on Stream阅读和流式编写而没有运气...

这是我使用WebClient而不是HttpClient的代码示例:

using System;
using System.IO;
using System.Net;
using System.Net.Http;

namespace StreamSpeedTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Sample video File
            const string fileUrl = "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi";
            // SaveTo location
            const string file = "/tmp/big_buck_bunny.avi";

            Console.WriteLine("Press any key to start ...");
            Console.ReadKey();

            var finfo = new FileInfo(file);

            if (finfo.Directory == null)
            {
                Console.WriteLine("Wrong file path!");
                return;
            }

            if (!finfo.Directory.Exists) finfo.Directory.Create();

            Console.WriteLine("Downloading data ...");

            using (var client = new WebClient {Proxy = null})
            {
                client.DownloadFile(fileUrl, file);
                client.DownloadFileCompleted += (sender,data)=> Console.WriteLine("Download File Completed!")
            }
            Console.ReadKey();
        }
    }
}