By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

Getting response when web file does not exist.

P: n/a
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?
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
try
{
....
HttpWebRequest ...
....
}
catch{}

"Peter Rilling" <pe***@nospam.rilling.net> schrieb im Newsbeitrag
news:OQ**************@TK2MSFTNGP09.phx.gbl...
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?

Nov 15 '05 #2

P: n/a
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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.