C#/ asp.net - 如何捕捉" System.Web.HttpException:请求超时"?

问题描述:

在我asp.net/c#项目,我现在用的iTextSharp的DLL来读取许多PDF文档中的文本,但有时我得到这个错误

In my asp.net/c# project I am using the iTextsharp dll to read the text from many pdf documents, but sometimes I get this error

System.Web.HttpException:请求超时

System.Web.HttpException: Request timed out.

不过,code,做它是:

But the code that does it is:

    public static bool does_pdf_have_keyword(string keyword, string pdf_src) 
    {
        try
        {
            PdfReader pdfReader = new PdfReader(pdf_src);
            string currentText;
            int count = pdfReader.NumberOfPages;
            for (int page = 1; page <= count; page++)
            {
                ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
                if (currentText.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) != -1) return true;
            }
            pdfReader.Close();
            return false;
        }
        catch
        {
            return false;
        }
    }

那么,为什么页面进入未处理的异常时,它在尝试渔获物和渔获应该捕获一切?

So why does the page go into an unhandled exception when it's in a try catch and the catch is supposed to catch everything?

我想原因你的尝试没有赶上这个例外是,你得到的例外是无法从code本身抛出,而是从服务器中。

I think the reason your try is not catching this exception is that the exception you're getting is not thrown from your code per se, but from the server.

想想这种方式:


  • 您$​​ C $ C运行良好,它只是需要很长的时间。

  • 服务器监视请求正在多久,杀死该请求,并抛出一个异常。

所以你的code实际上并不抛出该异常。

So your code doesn't actually throw that exception.

现在,如果你想了解它,或者记录它,你可以用你的的Global.asax $的的Application_Error 方法C $ C>文件(假设你访问它,我不知道如何与SharePoint工作)。

Now, if you want to find out about it or log it, you can use the Application_Error method in your Global.asax file (assuming you have access to it, I'm not sure how that works with SharePoint).

例如,在我的web项目之一,我想记录所有的错误,甚至被没有抓到的网址。所以,我做的是这样的:

For example, in one of my web projects, I wanted to log all errors, even ones that weren't caught. So what I do is something like this:

protected void Application_Error(object sender, EventArgs e) {
    //Log ALL uncaught exceptions
    Exception exc = Server.GetLastError();
    if (exc is HttpUnhandledException) {
        exc = Context.Error.InnerException;
    }
    //Log error here
}

我不知道有多少,你可以用它做其他比记录它。我不知道在哪里在页面生命周期发生这种情况,所以我不知道你是否可以做类似获取当前的HTTP请求对象并重定向用户。

I'm not sure there's much you can do with it other than log it. I don't know where in the page life cycle this occurs so I'm not sure if you could do something like get the current HTTP request object and redirect the user.

希望这有助于。