From 90ce1a6d000b049c0fe627c77fc06f7d3ec99048 Mon Sep 17 00:00:00 2001 From: karoltarasiuk Date: Mon, 9 Nov 2015 14:33:43 +1100 Subject: [PATCH] Added using clause to handle getting response and error streams to ensure proper disposing of the object in case of exceptions --- LearnositySDK/Request/Remote.cs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/LearnositySDK/Request/Remote.cs b/LearnositySDK/Request/Remote.cs index 8472ee6..25e4bb3 100755 --- a/LearnositySDK/Request/Remote.cs +++ b/LearnositySDK/Request/Remote.cs @@ -182,24 +182,31 @@ private Remote request(string url, string post = null, JsonObject options = null { Stopwatch timer = new Stopwatch(); timer.Start(); - this.result = this.hr.GetResponse(); + using (this.result = this.hr.GetResponse()) + { + using (StreamReader sr = new StreamReader(this.result.GetResponseStream())) + { + this.responseBody = sr.ReadToEnd(); + } + } this.status = "200"; timer.Stop(); this.time = timer.Elapsed.Seconds; } catch (WebException e) { - this.result = e.Response; + using (this.result = e.Response) + { + using (StreamReader sr = new StreamReader(this.result.GetResponseStream())) + { + this.responseBody = sr.ReadToEnd(); + } + } this.status = e.Status.ToString(); this.errorCode = this.status; this.errorMessage = e.Message; } - using (StreamReader sr = new StreamReader(this.result.GetResponseStream())) - { - this.responseBody = sr.ReadToEnd(); - } - this.process(); return this; }