|
Question: When I execute
HttpWebRequest.GetResponse on URL with 404 status, GetResponse returns
"The remote server returned an error: (404) Not Found." exception instead of response,
before I can retrieve the HttpStatusCode. Why is that? What can I do?
You can really test this strange "feature" on next source code. HttpWebRequest works with "404 Not Found" web response in the same way as with real errors - Connection failure, Timeout, NameResolutionFailure and other real errors. Next GetResponse function really returns Nothing when URL returns 404, 500 or other status codes, not regular HttpWebResponse object. To really get a response, you have to use System.Net.WebException instead of HttpWebRequest.GetResponse when the "404" exception occurs: See alsofor 'HttpWebRequest.GetResponse for "404 Not Found" and similar http status codes' article Copyright and use this code
The source code on this page and other samples at https://www.motobit.com/tips/
are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
or
2. Link this sample from you page.
<A Href="https://www.motobit.com/tips/detpg_net-getresponse-404/" Title="Really get a HttpWebResponse object from HttpWebRequest when http server answers with other status codes than 200 OK." >HttpWebRequest.GetResponse for "404 Not Found" and similar http status codes</A> Do you like it? Rate me on LinkedIn |
© 1996 - 2019 Antonin Foller, Motobit Software | About, Contacts