Open dgellow opened 8 years ago
The HttpWebResponse
is sitting on e.Response.res
, a private member of Response
on WebException
. It's not only private, it's protected or internal, so we can't expose it in a subclass. Would love to have access to it, as mapping a WebResponse
to an HttpWebResponse
using the public members is error-prone at best.
This issue should remain open. So far as I can tell, there is still no convenient way to retrieve the HTTP status code of a WebException. The least-worst way is to extract it from e.Message
, which is a string.
This is a major pitfall. If you Google for C# solutions, they'll tell you to downcast to HttpWebResponse
, which does not work for FSharp.Data's WebResponse
.
Not sure why it was closed, I don’t remember closing it? Pretty I didn’t interact with this issue since I opened it in 2016. Anyway, it’s reopened.
Confirming this is still an issue. Currently handling it like so:
try
// whatever logic
with
| :? Net.WebException as ex ->
if ex.Message.Contains("(404) Not Found") then // more logic
Hi,
I'm using
XmlProvider
to fetch and parse RSS feeds. I want to handle HTTP Errors but I cannot find a way to get the status code of awebexception.Response
. The C# approach is to cast theWebResponse
toHttpWebResponse
, that doesn't seem to work with the response fromMyXml.Load
.To be more explicit, look at the code below.
downcastWebResponse
tries to downcast theWebResponse
from the exception into anHttpWebResponse
. Unfortunately it fails thus returnsNone
. Without the guard, if I do something likelet resp = e.Response :?> HttpWebResponse
I get the errorUnhandled Exception: System.InvalidCastException: Specified cast is not valid.
.So, my question is: How can I get the status code from the exception response? And if I have to cast to
HttpWebResponse
, how can I do it?