So you get 404 in two cases: when either connection endpoint is down or when server actually responds with 404 NotFound. It would be nice to separate those two cases though, for example to display to the user the message that actually helps them fix the problem.
Fortunately you can tell those two situations apart by checking WebException.Response.ResponseUri - in case of connection failure it contains empty Uri object (not null, just empty). Here's the extension method I use to convert the exception to the one that makes more sense to me :
public static WebResponse SaneEndGetResponse(this WebRequest request, IAsyncResult asyncResult) { try { return request.EndGetResponse(asyncResult); } catch (WebException wex) { if (wex.Response != null && ((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound && wex.Response.ResponseUri != null && String.IsNullOrEmpty(wex.Response.ResponseUri.ToString())) { throw new WebException("Network error", WebExceptionStatus.ConnectFailure); } throw; } }
No comments:
Post a Comment