Httpclient获取下载速度
Httpclient getting the download speed
有人可以帮我一下吗?
我正在下载文件,我能够获得实际的下载速度。但如果速度大于 1mbps
,我想将 UNIT 从 KB/sec 更改为 MB/sec
我怎么知道它是否大于 1mbps 以便我的 UNIT 也会改变
这是我当前的代码。
我运行这是一个循环。
for (int i = 0; i < totalCount; i++)
{
string downloadUrl= LB.Items[i].toString();
double currentSize = TotalFileSize; //882672
string UNIT = "KB/s";
DateTime startTime = DateTime.Now;
await ProgressUpdate(downloadUrl, cts.Token); //the actual download
DateTime endTime = DateTime.Now;
double t = Math.Round(currentSize / 1024 / (endTime - startTime).TotalSeconds);
var downloadSpeed = (string.Format("Download Speed: {0}{1}", t, UNIT));
谢谢。
你可以使用一个小助手,例如:
var downloadSpeed = FormatSpeed(TotalFileSize, (endTime - startTime).TotalSeconds);
string FormatSpeed( double size, double tspan )
{
string message = "Download Speed: {0:N0} {1}";
if( size/tspan > 1024 * 1024 ) // MB
{
return string.Format(message, size/(1024*1204)/tspan, "MB/s");
}else if( size/tspan > 1024 ) // KB
{
return string.Format(message, size/(1024)/tspan, "KB/s");
}
else
{
return string.Format(message, size/tspan, "B/s");
}
}
看到了运行:https://dotnetfiddle.net/MjUW2M
输出:
Download Speed: 10 B/s
Download Speed: 100 B/s
Download Speed: 200 B/s
Download Speed: 100 KB/s
Download Speed: 83 MB/s
Download Speed: 9 MB/s
或略有不同:https://dotnetfiddle.net/aqanaT
string FormatSpeed( double size, double tspan )
{
string message = "Download Speed: ";
if( size/tspan > 1024 * 1024 ) // MB
{
return string.Format(message + "{0:N1} {1}", size/(1024*1204)/tspan, "MB/s");
}else if( size/tspan > 1024 ) // KB
{
return string.Format(message + "{0:N0} {1}", size/1024/tspan, "KB/s");
}
else
{
return string.Format(message + "{0:N1} {1}", size/1024/tspan, "KB/s");
}
输出:
Download Speed: 0.0 KB/s
Download Speed: 0.1 KB/s
Download Speed: 0.2 KB/s
Download Speed: 100 KB/s
Download Speed: 83.1 MB/s
Download Speed: 8.5 MB/s
有人可以帮我一下吗? 我正在下载文件,我能够获得实际的下载速度。但如果速度大于 1mbps
,我想将 UNIT 从 KB/sec 更改为 MB/sec我怎么知道它是否大于 1mbps 以便我的 UNIT 也会改变
这是我当前的代码。
我运行这是一个循环。
for (int i = 0; i < totalCount; i++)
{
string downloadUrl= LB.Items[i].toString();
double currentSize = TotalFileSize; //882672
string UNIT = "KB/s";
DateTime startTime = DateTime.Now;
await ProgressUpdate(downloadUrl, cts.Token); //the actual download
DateTime endTime = DateTime.Now;
double t = Math.Round(currentSize / 1024 / (endTime - startTime).TotalSeconds);
var downloadSpeed = (string.Format("Download Speed: {0}{1}", t, UNIT));
谢谢。
你可以使用一个小助手,例如:
var downloadSpeed = FormatSpeed(TotalFileSize, (endTime - startTime).TotalSeconds);
string FormatSpeed( double size, double tspan )
{
string message = "Download Speed: {0:N0} {1}";
if( size/tspan > 1024 * 1024 ) // MB
{
return string.Format(message, size/(1024*1204)/tspan, "MB/s");
}else if( size/tspan > 1024 ) // KB
{
return string.Format(message, size/(1024)/tspan, "KB/s");
}
else
{
return string.Format(message, size/tspan, "B/s");
}
}
看到了运行:https://dotnetfiddle.net/MjUW2M
输出:
Download Speed: 10 B/s
Download Speed: 100 B/s
Download Speed: 200 B/s
Download Speed: 100 KB/s
Download Speed: 83 MB/s
Download Speed: 9 MB/s
或略有不同:https://dotnetfiddle.net/aqanaT
string FormatSpeed( double size, double tspan )
{
string message = "Download Speed: ";
if( size/tspan > 1024 * 1024 ) // MB
{
return string.Format(message + "{0:N1} {1}", size/(1024*1204)/tspan, "MB/s");
}else if( size/tspan > 1024 ) // KB
{
return string.Format(message + "{0:N0} {1}", size/1024/tspan, "KB/s");
}
else
{
return string.Format(message + "{0:N1} {1}", size/1024/tspan, "KB/s");
}
输出:
Download Speed: 0.0 KB/s
Download Speed: 0.1 KB/s
Download Speed: 0.2 KB/s
Download Speed: 100 KB/s
Download Speed: 83.1 MB/s
Download Speed: 8.5 MB/s