Download an Upload Speed using HttpClient

Jassim Al Rahma 1,616 Reputation points
2022-11-16T04:11:38.73+00:00

Hi,

How can I get the download and upload speed using HttpClient?

Thanks,
Jassim

Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2022-11-16T09:36:51.807+00:00

    Hi @Jassim Al Rahma , you could try the following code to get what you wanted.

    HttpClient client = new HttpClient();  
      
                using (var stream = await client.GetStreamAsync(url))  
                {  
                    using (MemoryStream ms = new MemoryStream())  
                    {  
                        byte[] buffer = new byte[1024];  
                        Console.WriteLine("DownloadStarted");  
                        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 + "%");  
                        }  
                    }  
                }  
    

    Or you could refer to How to get accurate download/upload speed in C#.NET?.

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.