From b88f990b8276494eb8748f9a0322f0fbcacacd57 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 20 Apr 2013 15:28:58 -0400 Subject: [PATCH] refactor get method to use http response --- .../HttpClientManager/HttpClientManager.cs | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index f8edda203b..e605910cc8 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -104,6 +104,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager cancellationToken.ThrowIfCancellationRequested(); + var message = new HttpRequestMessage(HttpMethod.Get, url); + if (resourcePool != null) { await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); @@ -115,11 +117,14 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { cancellationToken.ThrowIfCancellationRequested(); - var msg = await GetHttpClient(GetHostFromUrl(url)).GetAsync(url, cancellationToken).ConfigureAwait(false); + using (var response = await GetHttpClient(GetHostFromUrl(url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false)) + { + EnsureSuccessStatusCode(response); - EnsureSuccessStatusCode(msg); + cancellationToken.ThrowIfCancellationRequested(); - return await msg.Content.ReadAsStreamAsync().ConfigureAwait(false); + return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + } } catch (OperationCanceledException ex) { @@ -131,6 +136,12 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager throw new HttpException(ex.Message, ex); } + catch (Exception ex) + { + _logger.ErrorException("Error getting response from " + url, ex); + + throw; + } finally { if (resourcePool != null) @@ -139,7 +150,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } } } - + /// /// Performs a POST request /// @@ -209,27 +220,12 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// progress /// /// - public Task GetTempFile(HttpRequestOptions options) - { - var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp"); - - return GetTempFile(options, tempFile); - } - - protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - - /// - /// Gets the temp file. - /// - /// The options. - /// The temp file. - /// Task{System.String}. - /// progress - /// - private async Task GetTempFile(HttpRequestOptions options, string tempFile) + public async Task GetTempFile(HttpRequestOptions options) { ValidateParams(options.Url, options.CancellationToken); + var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp"); + if (options.Progress == null) { throw new ArgumentNullException("progress"); @@ -250,7 +246,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } options.Progress.Report(0); - + _logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", options.Url, tempFile); try @@ -310,6 +306,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager return tempFile; } + protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + /// /// Handles the temp file exception. ///