Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 304
Arya Avatar asked Oct 25 '11 18:10

Arya


2 Answers

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;
like image 93
Damith Avatar answered Oct 21 '22 11:10

Damith


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);
like image 34
Jon Skeet Avatar answered Oct 21 '22 10:10

Jon Skeet