Check response time with HTTPWebRequest?

后端 未结 2 1705
猫巷女王i
猫巷女王i 2021-02-20 04:13

I\'m trying to find the performance of some of my proxies. I tried the Ping class in .net but it does not accept ports. Is there a way to check how long a response

相关标签:
2条回答
  • 2021-02-20 04:45
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
    System.Diagnostics.Stopwatch timer = new Stopwatch();
    
    timer.Start();
    
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    response.Close ();
    
    timer.Stop();
    
    TimeSpan timeTaken = timer.Elapsed;
    
    0 讨论(0)
  • 2021-02-20 04:46

    Why not just time it from the client side?

    WebRequest request = BuildRequest();
    Stopwatch sw = Stopwatch.StartNew();
    using (WebResponse response = request.GetResponse())
    {
        // Potentially fetch all the data here, in case it's streaming...
    }
    sw.Stop();
    Console.WriteLine("Request took {0}", sw.Elapsed);
    
    0 讨论(0)
提交回复
热议问题