public void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { RequestContext context = e.UserState as RequestContext; WebClient client = sender as WebClient; if (context != null) { var httpResponse = new HttpResponse { Body = "", Url = context.Url }; if (e.Error != null) { httpResponse.Error = e.Error; var webError = e.Error as WebException; if (webError != null) { if (webError.Status == WebExceptionStatus.ProtocolError && webError.Response != null) { var response = (HttpWebResponse)webError.Response; if (response.StatusCode != HttpStatusCode.NotFound) { using (var responseStream = response.GetResponseStream()) { httpResponse.Body = DecodeResponseStream(responseStream); ErrorResponse errorResponse = SerializationHelper.DeserializeJson<ErrorResponse>(httpResponse.Body); if (context.OnError != null) context.OnError(new ApiException(errorResponse.Error, e.Error, httpResponse.Url)); return; } } } if (context.OnError != null) context.OnError(new ApiException(e.Error)); return; } } //response.ParseRateLimit(client.ResponseHeaders); using (var memoryStream = new MemoryStream(e.Result)) { httpResponse.Body = DecodeResponseStream(memoryStream); } context.OnSuccess(httpResponse); } }
public HttpResponse MakeRequest(Uri url) { HttpResponse httpResponse = new HttpResponse { Url = url }; var request = WebRequest.Create(url) as HttpWebRequest; if (request != null) { request.UserAgent = "Stacky"; request.Accept = "gzip,deflate"; request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; try { using (var response = request.GetResponse() as HttpWebResponse) { httpResponse.ParseRateLimit(response.Headers); using (var responseStream = response.GetResponseStream()) { var reader = new StreamReader(responseStream); httpResponse.Body = reader.ReadToEnd(); } } } catch (WebException e) { httpResponse.Error = e; if (e.Status == WebExceptionStatus.ProtocolError && e.Response != null) { var response = (HttpWebResponse)e.Response; if (response.StatusCode != HttpStatusCode.NotFound) { httpResponse.ParseRateLimit(response.Headers); using (var responseStream = response.GetResponseStream()) { using (var reader = new StreamReader(responseStream)) { httpResponse.Body = reader.ReadToEnd(); } } } } } } return httpResponse; }
public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { RequestContext context = e.UserState as RequestContext; WebClient client = sender as WebClient; if (context != null) { var httpResponse = new HttpResponse { Url = context.Url }; if (e.Error != null) { httpResponse.Error = e.Error; var webError = e.Error as WebException; if (context.OnError != null) context.OnError(new ApiException(e.Error, httpResponse.Url)); return; } //TODO: Figure out how to get the ResponseHeaders in Silverlight //response.ParseRateLimit(client.ResponseHeaders); httpResponse.Body = e.Result; context.OnSuccess(httpResponse); } }
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { string data; using (var gzip = new GZipInputStream(e.Result)) using (var reader = new StreamReader(gzip)) { data = reader.ReadToEnd(); } RequestContext context = e.UserState as RequestContext; WebClient client = sender as WebClient; if (context != null) { var httpResponse = new HttpResponse { Url = context.Url }; if (e.Error != null) { httpResponse.Error = e.Error; var webError = e.Error as WebException; if (context.OnError != null) context.OnError(new ApiException(e.Error, httpResponse.Url)); return; } httpResponse.Body = data; context.OnSuccess(httpResponse); } }