Asp.net HttpWebResponse - how can I not depend on WebException for flow control?

Posted by Campos on Stack Overflow See other posts from Stack Overflow or by Campos
Published on 2010-06-09T12:56:38Z Indexed on 2010/06/09 14:32 UTC
Read the original article Hit count: 454

I need to check whether the request will return a 500 Server Internal Error or not (so getting the error is expected). I'm doing this:

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
request.Method = "GET";  
HttpWebResponse response = request.GetResponse() as HttpWebResponse; 

if (response.StatusCode == HttpStatusCode.OK)
     return true;
else 
    return false;

But when I get the 500 Internal Server Error, a WebException is thrown, and I don't want to depend on it to control the application flow - how can this be done?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about exception