Skip to content

Commit

Permalink
Added using clause to handle getting response and error streams to en…
Browse files Browse the repository at this point in the history
…sure

proper disposing of the object in case of exceptions
  • Loading branch information
karoltarasiuk committed Nov 9, 2015
1 parent deae5e7 commit 90ce1a6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions LearnositySDK/Request/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 90ce1a6

Please sign in to comment.