dispose mem-critical objects; fixes #7

This commit is contained in:
2014-02-13 00:19:11 +01:00
parent ac3b02faf4
commit fe7f5640d0
2 changed files with 27 additions and 15 deletions
+3
View File
@@ -1,9 +1,12 @@
using System.IO;
using System.Net.Http;
namespace ReadSharp
{
internal class Response
{
public HttpResponseMessage RawResponse { get; set; }
public Stream Stream { get; set; }
public string Charset { get; set; }
+24 -15
View File
@@ -158,6 +158,11 @@ namespace ReadSharp
{
throw new ReadException(exc.Message);
}
finally
{
response.RawResponse.Dispose();
response.Stream.Dispose();
}
// get images from article
int id = 1;
@@ -293,34 +298,38 @@ namespace ReadSharp
/// </exception>
private async Task<Response> Request(Uri uri, CancellationToken cancellationToken)
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
HttpResponseMessage response = null;
// make async request
try
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri))
{
response = await _httpClient.SendAsync(request, cancellationToken);
}
catch (HttpRequestException exc)
{
throw new ReadException(exc.Message);
}
// make async request
try
{
response = await _httpClient.SendAsync(request, cancellationToken);
}
catch (HttpRequestException exc)
{
throw new ReadException(exc.Message);
}
// validate HTTP response
if (response.StatusCode != HttpStatusCode.OK)
{
string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode);
// validate HTTP response
if (response.StatusCode != HttpStatusCode.OK)
{
string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode);
throw new ReadException(exceptionString);
throw new ReadException(exceptionString);
}
}
// read response
Stream responseStream = await response.Content.ReadAsStreamAsync();
string charset = response.Content.Headers.ContentType.CharSet;
return new Response()
{
RawResponse = response,
Stream = responseStream,
Charset = response.Content.Headers.ContentType.CharSet
Charset = charset
};
}
}