Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中检查Httpclient下载速度?_C#_Httpclient - Fatal编程技术网

如何在C#中检查Httpclient下载速度?

如何在C#中检查Httpclient下载速度?,c#,httpclient,C#,Httpclient,Webclient类能够通过以下代码测量速度: ConcurrentQueue<long> bytes = new ConcurrentQueue<long>(); long before = 0; private async void WebClientDownload(string url,string filepath) { using (WebClient client = new WebClient())

Webclient类能够通过以下代码测量速度:

    ConcurrentQueue<long> bytes = new ConcurrentQueue<long>();
    long before = 0;
    private async void WebClientDownload(string url,string filepath)
    {
        using (WebClient client = new WebClient())
        {
            client.DownloadProgressChanged += (sender, e) =>
            {
                bytes.Enqueue(e.BytesReceived - before);
                before = e.BytesReceived;                   
            };
            await client.DownloadFileTaskAsync(url, filepath);
        }
    }
    private async void MeasureSpeed()
    {
        while(true)
        {
            long val,sum = 0;
            while (bytes.TryDequeue(out val)) sum += val;
            sum /= 1024;
            //Print : Speed= sum KB/s
            await Task.Delay(1000);
        }
    }
ConcurrentQueue bytes=new ConcurrentQueue();
很久以前=0;
私有异步void WebClient下载(字符串url、字符串文件路径)
{
使用(WebClient=newWebClient())
{
client.DownloadProgressChanged+=(发件人,e)=>
{
字节排队(例如,字节接收-之前);
before=e.收到的字节数;
};
wait client.DownloadFileTaskAsync(url,文件路径);
}
}
私有异步void MeasureSpeed()
{
while(true)
{
长值,和=0;
而(bytes.TryDequeue(out val))sum+=val;
总和/=1024;
//打印:速度=总和KB/s
等待任务。延迟(1000);
}
}
然而,由于Httpclient类似乎更快,我想使用这个类。与WebClient不同,HttpClient不知道在没有ProgressChanged事件的情况下,到目前为止收到的字节数


HttpClient中是否有办法知道到目前为止已收到多少字节,例如WebClient的ProgressChanged事件?

谢谢您的回答。这与Webclient的ProgressChanged具有相同的效果。那么您到底想要什么?是的。我已经根据自己的情况修改了此代码。但除此之外,还有一件事我想知道。使用GetResponseStream在内存中的流上读取异步意味着从web下载资源?在我写这篇文章之前,当我使用GetResponseStream获得流时,我以为我不再与web通信了。
  HttpClient client = new HttpClient();

            using (var stream = await client.GetStreamAsync(url))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] buffer = new byte[1024];
                    Console.WriteLine("Download Started");
                    totalBytes = client.MaxResponseContentBufferSize;

                    for (; ; )
                    {
                        int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            await Task.Yield();
                            break;
                        }

                        receivedBytes += bytesRead;

                        int received = unchecked((int)receivedBytes);
                        int total = unchecked((int)totalBytes);

                        double percentage = ((float)received) / total;
                        Console.WriteLine(received / (1024) + "Kb / " + total / (1024 )+" Kb");
                        Console.WriteLine("Completed : " + percentage + "%");
                    }
                }
            }