简体   繁体   English

测量互联网速度而不保存远程文件

[英]Measuring internet speed without saving remote file

I am writing an internet speed measurer. 我正在写一个互联网速度测量器。 I want to measure internet speed without saving remote file to either file or memory - I need fetch the data and forget it, so it seems that WebClient and StreamReader is not good for it (maybe i should inherit them and override some private methods). 我想测量互联网速度而不将远程文件保存到文件或内存 - 我需要获取数据并忘记它,所以似乎WebClient和StreamReader不利于它(也许我应该继承它们并覆盖一些私有方法)。 How to make it? 怎么做?

I think that when writing such a system you should also select exactly which size of file you want to download. 我认为在编写这样的系统时,您还应该准确选择要下载的文件大小。 Anyway, you can maybe download parts of the file if the "very large" or "infinite" size is a problem. 无论如何,如果“非常大”或“无限”大小是个问题,你可以下载部分文件。 You can maybe use HttpWebRequest.AddRange http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.addrange.aspx 您可以使用HttpWebRequest.AddRange http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.addrange.aspx

You can use System.Net.WebRequest to measure the throughput, using asynchronous calls to capture the data as it is being read. 您可以使用System.Net.WebRequest来测量吞吐量,使用异步调用来捕获正在读取的数据。

The MSDN example code for WebRequest.BeginGetResponse shows one method for using the asynchronous methods to read the data from a remote file as it is being received. WebRequest.BeginGetResponse的MSDN示例代码显示了一种使用异步方法在接收远程文件时读取数据的方法。 The example stores the response data in a StringBuilder , but you can skip that since you're not actually interested in the data itself. 该示例将响应数据存储在StringBuilder ,但您可以跳过它,因为您实际上并不对数据本身感兴趣。

I added some timing code to their example and tested it against a couple of large file downloads, seems to do the job you need it for. 我在他们的示例中添加了一些计时代码,并针对几个大文件下载进行了测试,似乎完成了您需要的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM