Eric's answer does not work on Windows Phone as it is. The following does:
class WebClientEx : WebClient { private WebResponse m_Resp = null; protected override WebResponse GetWebResponse(WebRequest Req, IAsyncResult ar) { try { this.m_Resp = base.GetWebResponse(request); } catch (WebException ex) { if (this.m_Resp == null) this.m_Resp = ex.Response; } return this.m_Resp; } public HttpStatusCode StatusCode { get { if (m_Resp != null && m_Resp is HttpWebResponse) return (m_Resp as HttpWebResponse).StatusCode; else return HttpStatusCode.OK; } } }
At least this happens when using OpenReadAsync ; rigorous testing is strongly recommended for other xxxAsync methods. The framework calls GetWebResponse somewhere along the code path; all you have to do is grab and cache the response object.
In this fragment, the backup code is 200, because the genuine HTTP errors are 500, 404, etc. - in any case are considered exceptions. The purpose of this trick is to capture error-free codes, in my specific case 304 (not changed). Thus, the fallback assumes that if the status code is somehow unavailable, at least it is not erroneous.
Seva Alekseyev Nov 13 '13 at 16:42 2013-11-13 16:42
source share