使用HTTPWebRequest检查响应时间?

时间:2011-10-25 18:17:01

标签: c# .net httpwebrequest

我正试图找到一些代理的性能。我在.net中尝试了Ping类,但它不接受端口。有没有办法检查httpwebrequest的响应时间?

2 个答案:

答案 0 :(得分:22)

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;

答案 1 :(得分:17)

为什么不从客户端计算它?

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