我正在从网站读取数据,如下所示:
webRequestObj = (HttpWebRequest)WebRequest.Create("http://localhost/503errorPage.php");
theResponse = webRequestObj.GetResponse();
theResponseStream = theResponse.GetResponseStream();
theStreamReader = new StreamReader(theResponseStream);
theWholePage = theStreamReader.ReadToEnd();
如果我进入一个有 503 错误的页面,该行:
theResponse = webRequestObj.GetResponse();
抛出一个 WebException:
The remote server returned an error: (503) Server Unavailable.
有没有办法检查网站是否返回 503 错误,而不是将所有内容都包装在 try/catch 块中并等待异常?我每秒做很多更新,如果可能的话,我想避免尝试/捕获。