简体   繁体   中英

Check response time with HTTPWebRequest?

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 took with httpwebrequest ?

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;

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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