refactored GetMemoryStream to use Get
This commit is contained in:
parent
b88f990b82
commit
9c1e771af5
|
@ -363,68 +363,25 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
|
||||||
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
var message = new HttpRequestMessage(HttpMethod.Get, url);
|
_logger.Info("HttpClientManager.GetMemoryStream url: {0}", url);
|
||||||
|
|
||||||
if (resourcePool != null)
|
|
||||||
{
|
|
||||||
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
var ms = new MemoryStream();
|
var ms = new MemoryStream();
|
||||||
|
|
||||||
_logger.Info("HttpClientManager.GetMemoryStream url: {0}", url);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
using (var stream = await Get(url, resourcePool, cancellationToken).ConfigureAwait(false))
|
||||||
|
|
||||||
using (var response = await GetHttpClient(GetHostFromUrl(url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
|
|
||||||
{
|
{
|
||||||
EnsureSuccessStatusCode(response);
|
await stream.CopyToAsync(ms, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
|
|
||||||
{
|
|
||||||
await stream.CopyToAsync(ms, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ms.Position = 0;
|
|
||||||
|
|
||||||
return ms;
|
return ms;
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException ex)
|
catch
|
||||||
{
|
{
|
||||||
ms.Dispose();
|
ms.Dispose();
|
||||||
|
|
||||||
throw GetCancellationException(url, cancellationToken, ex);
|
|
||||||
}
|
|
||||||
catch (HttpRequestException ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error getting response from " + url, ex);
|
|
||||||
|
|
||||||
ms.Dispose();
|
|
||||||
|
|
||||||
throw new HttpException(ex.Message, ex);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error getting response from " + url, ex);
|
|
||||||
|
|
||||||
ms.Dispose();
|
|
||||||
|
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (resourcePool != null)
|
|
||||||
{
|
|
||||||
resourcePool.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user