Peter Rilling wrote:
I am trying to use an HttpWebRequest object to get resource from a
website. Now, some of the files that I try to get may not exist. I
would like to check for that condition. When I call GetResponse on
files that do not exist, I get an exception. What I would like is
not to have the exception but allow me to check that StatusCode
property for the 402 condition. How can I do this?
Peter,
HTTP protocol errors such as 404 are escalated by the framework as
WebException instances with the Status property set to
WebExceptionStatus.ProtocolError. In that case, the WebException will have a
valid WebResponse instance in its Response property:
try {
// Do WebRequest
}
catch (WebException ex) {
if (ex.Status == WebExceptionStatus.ProtocolError) {
HttpWebResponse response = ex.Response as HttpWebResponse;
if (response != null) {
// Process response, look for HTTP status code, etc.
}
}
}
Cheers,
--
Joerg Jooss
jo*********@gmx.net