【问题标题】:Catch timeout with HttpException使用 HttpException 捕获超时
【发布时间】:2016-02-06 16:38:57
【问题描述】:

如果用户的上传时间超过 15 分钟,我们有一个图片上传页面会超时。

我们正在捕获超时发生的 HttpException。但是我们怎么知道异常是因为超时而发生的,所以我们可以返回特定的消息呢?

我们的代码:

try
{
    // do stuff here
}
catch (HttpException ex)
{
    // What can we check to know if this is a timeout exception?
    // if (ex == TimeOutError)
    // {
    //      return "Took too long. Please upload a smaller image.";
    // }
    return "Error with image. Try again.";
}
catch (Exception ex)
{
    return "Error with image. Try again.";
}

以及超时错误是什么样子的:

System.Web.HttpException (0x80004005): Request timed out.
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.GetMultipartContent()
at System.Web.HttpRequest.FillInFormCollection()
at System.Web.HttpRequest.EnsureForm()
at System.Web.HttpRequest.get_Form()
at MyStore.upload.ProcessRequest(HttpContext context) 

ex.ErrorCode=-2147467259
ex.GetHttpCode=500
ex.WebEventCode=0

我很犹豫要不要简单地做一个if 语句来比较上面的错误代码。

HttpCode 500 似乎是一个通用的 Internal Server Error 代码,它可能不仅仅发生超时异常。

ErrorCode -2147467259 是我不熟悉的东西。如果该数字对于超时错误保持不变,并且永远不会发生非超时异常,那么我可以对这个数字进行if 比较。

我认为必须有一种简单的方法来知道 HttpException 是否是超时异常,例如:

if (ex == TimeoutError) // what should this be?

更新:

我刚刚尝试捕捉TimeoutException,如下所示,但它仍然只被HttpException捕捉。

try
{
    // do stuff here
}
catch (TimeoutException ex)
{
    // Timeout doesn't get caught. Must be a different type of timeout.
    // So far, timeout is only caught by HttpException.
    return "Took too long. Please upload a smaller image.";
}
catch (HttpException ex)
{
    // What can we check to know if this is a timeout exception?
    // if (ex == TimeOutError)
    // {
    //      return "Took too long. Please upload a smaller image.";
    // }
    return "Error with image. Try again.";
}
catch (Exception ex)
{
    return "Error with image. Try again.";
}

【问题讨论】:

  • ex typeof TimeoutException
  • @YaWang 你应该把它作为答案。

标签: c# asp.net httpexception


【解决方案1】:

你需要使用

ex.getType() is subClassException

因为 TimeoutException 是一个子类。如果它是一个可能的抛出,那将是如何捕捉这种类型的执行......

尽管 httpexpection 总是会被抛出,即使它确实超时了(请参阅每个方法抛出的 https://msdn.microsoft.com/en-us/library/system.web.httprequest(v=vs.110).aspx)。所以你需要做类似的事情,

if(e.Message.Contains("timed out"))
   //Do something because it timed out

【讨论】:

  • 不编译。如果这是VB代码,请用C#回答。当问题具有特定于语言的标签时,在本例中为 C#,这就是他们要求的语言。
  • 您的编辑仍然无法编译。我假设您的意思是代码为ex.GetType() is TimeoutException。注意大写的GetType() 和实际的TimeoutException。无论如何,即使是固定代码也不起作用。编译警告:The given expression is never of the provided ('System.TimeoutException') type.
  • @DougS 我看到了你的问题......即使它确实超时了,它也没有抛出 TimeoutException。它从不这样做,它总是只抛出一个 HTTP 异常,以告诉您需要获取异常的消息并执行 .Contains 字符串消息
  • 伙计,必须通过e.Message.Contains("timed out") 才能知道它是否可能是超时错误,这太难看了。希望有更好的方法。似乎必须有。我会让这个问题搁置一段时间,并继续自己研究,看看是否有更好的解决方案。感谢您的想法。
  • 正如问题中提到的,返回的 HttpCode 500 似乎只是一个通用的 Internal Server Error,可能会出现多种类型的错误。 -2147467259的另一个ErrorCode我找不到任何信息,或者它是否仅用于超时错误。
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 2020-05-31
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多