首先,感谢您能给我的帮助。
好吧,首先,请原谅我的小知识,我正在学习,所以请原谅我的无知。
我有一个调用服务的方法,并且假设如果接收到 200 范围以外的任何响应,它将被捕获。好吧,我需要在那个捕获中从该响应的标头(或某处)中检索服务响应所花费的时间。
我不能使用计时器或类似的东西(这是我首先想到的)。
问题是,我不知道如何从异常中恢复这些数据。
非常感谢你!
{
try
{
// Reading the http response
using (HttpWebResponse webResponse = httpWebRequest.GetResponse () as HttpWebResponse)
{
///// Call to endpoint
}
}
catch (WebException ex)
{
//// Here I need to retrieve the time it took for the service to respond (with the error)
}
return response;
} ```
为 kshkarin 提到的内容添加一个示例:
var watch = System.Diagnostics.Stopwatch.StartNew();
try
{
var request = WebRequest.Create("http://www.google.com");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream answer = response.GetResponseStream())
{
// do something
watch.Stop();
Console.WriteLine($"Success at {watch.ElapsedMilliseconds}");
}
}
}
catch (WebException e)
{
// If we got here, it was a timeout exception.
watch.Stop();
Console.WriteLine($"Error occurred at {watch.ElapsedMilliseconds} \n {e}");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句