简体   繁体   English

我应该在WebRequest调用中注意哪些异常?

[英]Which exceptions should I watch for in a WebRequest call?

I'm working on a small but vital batch application which has a step in it to download an image from a remote website I don't have any control over. 我正在开发一个小型但至关重要的批处理应用程序,该应用程序中有一个步骤可以从我无法控制的远程网站下载图像。 This is quite simple and I got it working already but I'd like some input on the error-handling. 这很简单,我已经开始工作了,但是我想在错误处理方面提供一些输入。

The remote server doesn't always have the image requested so a 404 Not Found is actually alright, but it's vital to catch all other communications errors, like timeouts and connection errors so that it can be put on a retry queue. 远程服务器并不总是有请求的映像,因此404 Not Found实际上没问题,但是捕获所有其他通信错误(如超时和连接错误)至关重要,以便将其放置在重试队列中。 I'd love to get suggestions about the exception-handring, anything in particular I should think about? 我很想获得有关异常处理的建议,我应该特别考虑些什么? Any other exception types that I should filter on? 还有其他我应该过滤的异常类型吗?

try 
{
    // webrequest.getresponse(), read file and return image
}
catch (WebException webEx) 
{
    // check the WebException/http status code and act on certain errors
    // if 404 image/file not found - ok, just return
    // other error - log and put on retry queue
}
catch (Exception ex) 
{
    // some other major failure, log and alert
}

Please see HttpWebRequest.GetResponse . 请参阅HttpWebRequest.GetResponse The documentation lists these potential exceptions: 该文档列出了这些潜在的例外情况:

  • InvalidOpertationException
  • ProtocolViolationException
  • NotSupportedException
  • WebException

See this . 看到这个 HttpWebRequest is ridden with so-called "vexing exceptions", so you'll have to watch out for them. HttpWebRequest充满了所谓的“令人烦恼的异常”,因此您必须提防它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM