使用raspberry pi上的c#HttpClient下载文件
我正试图下载一个文件到一个usb磁盘与单声道的覆盆子圆周率 我正在使用Framework4.5和HttpClient类来完成这项任务,看起来一切正常,除了CPU正在增长到100%。这是正常的吗?或者我做错了什么?如果我尝试用curl下载同一个文件,下载文件时CPU的运行速度大约为5%-10% 这是我的代码示例:使用raspberry pi上的c#HttpClient下载文件,c#,mono,httpclient,C#,Mono,Httpclient,我正试图下载一个文件到一个usb磁盘与单声道的覆盆子圆周率 我正在使用Framework4.5和HttpClient类来完成这项任务,看起来一切正常,除了CPU正在增长到100%。这是正常的吗?或者我做错了什么?如果我尝试用curl下载同一个文件,下载文件时CPU的运行速度大约为5%-10% 这是我的代码示例: using System; using System.IO; using System.Net.Http; namespace StreamSpeedTest { class
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");
}
}
}
}
我还尝试了异步流读和流写,但没有成功
这是我使用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();
}
}
}
为什么不使用?WebClient.DownloadFile也有同样的问题,CPU运行在90-100%之间,我在HttpClient和WebClient之间没有首选项,但两者都有同样的CPU负载问题。这很奇怪。请向我们展示您使用DownloadFile的实现对不起,这是实现…所以第二个实现占用了您100%的CPU?为什么不使用?对于WebClient.DownloadFile也有同样的问题,CPU的运行速度为90-100%,我在HttpClient和WebClient之间没有首选项,但两者在cpu负载方面都有同样的问题,这很奇怪。请用DownloadFileSorry向我们展示您的实现,这就是实现…那么第二个实现占用了您100%的CPU?