@ -132,7 +132,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
#if __MonoCS__
#if __MonoCS__
return GetMonoRequest ( options , method , enableHttpCompression ) ;
return GetMonoRequest ( options , method , enableHttpCompression ) ;
# endif
# endif
var request = HttpWebRequest . CreateHttp ( options . Url ) ;
var request = HttpWebRequest . CreateHttp ( options . Url ) ;
if ( ! string . IsNullOrEmpty ( options . AcceptHeader ) )
if ( ! string . IsNullOrEmpty ( options . AcceptHeader ) )
@ -172,9 +172,64 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
/// <returns>Task{HttpResponseInfo}.</returns>
/// <returns>Task{HttpResponseInfo}.</returns>
/// <exception cref="HttpException">
/// <exception cref="HttpException">
/// </exception>
/// </exception>
public async Task < HttpResponseInfo > GetResponse ( HttpRequestOptions options )
public Task < HttpResponseInfo > GetResponse ( HttpRequestOptions options )
{
return SendAsync ( options , "GET" ) ;
}
/// <summary>
/// Performs a GET request and returns the resulting stream
/// </summary>
/// <param name="options">The options.</param>
/// <returns>Task{Stream}.</returns>
/// <exception cref="HttpException"></exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task < Stream > Get ( HttpRequestOptions options )
{
var response = await GetResponse ( options ) . ConfigureAwait ( false ) ;
return response . Content ;
}
/// <summary>
/// Performs a GET request and returns the resulting stream
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="resourcePool">The resource pool.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
public Task < Stream > Get ( string url , SemaphoreSlim resourcePool , CancellationToken cancellationToken )
{
{
ValidateParams ( options . Url , options . CancellationToken ) ;
return Get ( new HttpRequestOptions
{
Url = url ,
ResourcePool = resourcePool ,
CancellationToken = cancellationToken ,
} ) ;
}
/// <summary>
/// Gets the specified URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
public Task < Stream > Get ( string url , CancellationToken cancellationToken )
{
return Get ( url , null , cancellationToken ) ;
}
/// <summary>
/// send as an asynchronous operation.
/// </summary>
/// <param name="options">The options.</param>
/// <param name="httpMethod">The HTTP method.</param>
/// <returns>Task{HttpResponseInfo}.</returns>
/// <exception cref="HttpException">
/// </exception>
private async Task < HttpResponseInfo > SendAsync ( HttpRequestOptions options , string httpMethod )
{
ValidateParams ( options ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
@ -185,7 +240,17 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
throw new HttpException ( string . Format ( "Cancelling connection to {0} due to a previous timeout." , options . Url ) ) { IsTimedOut = true } ;
throw new HttpException ( string . Format ( "Cancelling connection to {0} due to a previous timeout." , options . Url ) ) { IsTimedOut = true } ;
}
}
var httpWebRequest = GetRequest ( options , "GET" , options . EnableHttpCompression ) ;
var httpWebRequest = GetRequest ( options , httpMethod , options . EnableHttpCompression ) ;
if ( ! string . IsNullOrEmpty ( options . RequestContent ) | | string . Equals ( httpMethod , "post" , StringComparison . OrdinalIgnoreCase ) )
{
var content = options . RequestContent ? ? string . Empty ;
var bytes = Encoding . UTF8 . GetBytes ( content ) ;
httpWebRequest . ContentType = options . RequestContentType ? ? "application/x-www-form-urlencoded" ;
httpWebRequest . ContentLength = bytes . Length ;
httpWebRequest . GetRequestStream ( ) . Write ( bytes , 0 , bytes . Length ) ;
}
if ( options . ResourcePool ! = null )
if ( options . ResourcePool ! = null )
{
{
@ -202,7 +267,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
throw new HttpException ( string . Format ( "Connection to {0} timed out" , options . Url ) ) { IsTimedOut = true } ;
throw new HttpException ( string . Format ( "Connection to {0} timed out" , options . Url ) ) { IsTimedOut = true } ;
}
}
_logger . Info ( "HttpClientManager .GET url: {0}" , options . Url ) ;
_logger . Info ( "HttpClientManager {0}: {1}", httpMethod . ToUpper ( ) , options . Url ) ;
try
try
{
{
@ -275,46 +340,9 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
}
}
}
}
/// <summary>
public Task < HttpResponseInfo > Post ( HttpRequestOptions options )
/// Performs a GET request and returns the resulting stream
/// </summary>
/// <param name="options">The options.</param>
/// <returns>Task{Stream}.</returns>
/// <exception cref="HttpException"></exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task < Stream > Get ( HttpRequestOptions options )
{
{
var response = await GetResponse ( options ) . ConfigureAwait ( false ) ;
return SendAsync ( options , "POST" ) ;
return response . Content ;
}
/// <summary>
/// Performs a GET request and returns the resulting stream
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="resourcePool">The resource pool.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
public Task < Stream > Get ( string url , SemaphoreSlim resourcePool , CancellationToken cancellationToken )
{
return Get ( new HttpRequestOptions
{
Url = url ,
ResourcePool = resourcePool ,
CancellationToken = cancellationToken ,
} ) ;
}
/// <summary>
/// Gets the specified URL.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
public Task < Stream > Get ( string url , CancellationToken cancellationToken )
{
return Get ( url , null , cancellationToken ) ;
}
}
/// <summary>
/// <summary>
@ -329,82 +357,15 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task < Stream > Post ( HttpRequestOptions options , Dictionary < string , string > postData )
public async Task < Stream > Post ( HttpRequestOptions options , Dictionary < string , string > postData )
{
{
ValidateParams ( options . Url , options . CancellationToken ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
var httpWebRequest = GetRequest ( options , "POST" , options . EnableHttpCompression ) ;
var strings = postData . Keys . Select ( key = > string . Format ( "{0}={1}" , key , postData [ key ] ) ) ;
var strings = postData . Keys . Select ( key = > string . Format ( "{0}={1}" , key , postData [ key ] ) ) ;
var postContent = string . Join ( "&" , strings . ToArray ( ) ) ;
var postContent = string . Join ( "&" , strings . ToArray ( ) ) ;
var bytes = Encoding . UTF8 . GetBytes ( postContent ) ;
httpWebRequest . ContentType = "application/x-www-form-urlencoded" ;
httpWebRequest . ContentLength = bytes . Length ;
httpWebRequest . GetRequestStream ( ) . Write ( bytes , 0 , bytes . Length ) ;
if ( options . ResourcePool ! = null )
{
await options . ResourcePool . WaitAsync ( options . CancellationToken ) . ConfigureAwait ( false ) ;
}
_logger . Info ( "HttpClientManager.POST url: {0}" , options . Url ) ;
try
{
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
using ( var response = await httpWebRequest . GetResponseAsync ( ) . ConfigureAwait ( false ) )
{
var httpResponse = ( HttpWebResponse ) response ;
EnsureSuccessStatusCode ( httpResponse ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
using ( var stream = httpResponse . GetResponseStream ( ) )
{
var memoryStream = new MemoryStream ( ) ;
await stream . CopyToAsync ( memoryStream ) . ConfigureAwait ( false ) ;
memoryStream . Position = 0 ;
options . RequestContent = postContent ;
options . RequestContentType = "application/x-www-form-urlencoded" ;
return memoryStream ;
var response = await Post ( options ) . ConfigureAwait ( false ) ;
}
}
}
catch ( OperationCanceledException ex )
{
var exception = GetCancellationException ( options . Url , options . CancellationToken , ex ) ;
throw exception ;
return response . Content ;
}
catch ( HttpRequestException ex )
{
_logger . ErrorException ( "Error getting response from " + options . Url , ex ) ;
throw new HttpException ( ex . Message , ex ) ;
}
catch ( WebException ex )
{
_logger . ErrorException ( "Error getting response from " + options . Url , ex ) ;
throw new HttpException ( ex . Message , ex ) ;
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error getting response from " + options . Url , ex ) ;
throw ;
}
finally
{
if ( options . ResourcePool ! = null )
{
options . ResourcePool . Release ( ) ;
}
}
}
}
/// <summary>
/// <summary>
@ -443,7 +404,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
public async Task < HttpResponseInfo > GetTempFileResponse ( HttpRequestOptions options )
public async Task < HttpResponseInfo > GetTempFileResponse ( HttpRequestOptions options )
{
{
ValidateParams ( options .Url , options . CancellationToken );
ValidateParams ( options );
Directory . CreateDirectory ( _appPaths . TempDirectory ) ;
Directory . CreateDirectory ( _appPaths . TempDirectory ) ;
@ -592,7 +553,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{
{
return new HttpException ( ex . Message , ex ) ;
return new HttpException ( ex . Message , ex ) ;
}
}
return ex ;
return ex ;
}
}
@ -608,17 +569,11 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
}
}
}
}
/// <summary>
private void ValidateParams ( HttpRequestOptions options )
/// Validates the params.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <exception cref="System.ArgumentNullException">url</exception>
private void ValidateParams ( string url , CancellationToken cancellationToken )
{
{
if ( string . IsNullOrEmpty ( u rl) )
if ( string . IsNullOrEmpty ( options . Url ) )
{
{
throw new ArgumentNullException ( " url ") ;
throw new ArgumentNullException ( "options" ) ;
}
}
}
}