471,354 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

WebClient and the HTTP error code

I am using the "WebClient" to post some data to a server. It could
happen that the server answers "404 Not found". But how can I take and
know the error code of the HTTP?

It seems that "WebClient" doesn't expose any methods to understand this
code..

Feb 7 '06 #1
2 18318
RicercatoreSbadato,

I believe an exception is thrown in this case. When this happens, check
the Response property of the exception. This is a HttpWebResponse (in the
case of HTTP transfers). You can then check the StatusCode property and
look for the 404 response.

You might be better off using HttpWebRequest and HttpWebResponse
directly in this case, since you can check the response code without having
to catch an exception.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"RicercatoreSbadato" <em***********@yahoo.it> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
I am using the "WebClient" to post some data to a server. It could
happen that the server answers "404 Not found". But how can I take and
know the error code of the HTTP?

It seems that "WebClient" doesn't expose any methods to understand this
code..

Feb 7 '06 #2
Thank you very much, now it works!

[...]
catch(WebException webex)
{
HttpWebResponse webResp = (HttpWebResponse) webex.Response;

switch (webResp.StatusCode)
{
case HttpStatusCode.NotFound: // 404
break;
case HttpStatusCode.InternalServerError: // 500
break;
default:
break;
}
}
[...]

Feb 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Justin Malloy | last post: by
1 post views Thread by yyy | last post: by
6 posts views Thread by Chris | last post: by
3 posts views Thread by Manuel | last post: by
6 posts views Thread by =?Utf-8?B?U2NvdHQgVHJpY2s=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.