@ -172,107 +172,9 @@ 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 )
{
{
ValidateParams ( options . Url , options . CancellationToken ) ;
return SendAsync ( options , "GET" ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
var client = GetHttpClient ( GetHostFromUrl ( options . Url ) , options . EnableHttpCompression ) ;
if ( ( DateTime . UtcNow - client . LastTimeout ) . TotalSeconds < TimeoutSeconds )
{
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 ) ;
if ( options . ResourcePool ! = null )
{
await options . ResourcePool . WaitAsync ( options . CancellationToken ) . ConfigureAwait ( false ) ;
}
if ( ( DateTime . UtcNow - client . LastTimeout ) . TotalSeconds < TimeoutSeconds )
{
if ( options . ResourcePool ! = null )
{
options . ResourcePool . Release ( ) ;
}
throw new HttpException ( string . Format ( "Connection to {0} timed out" , options . Url ) ) { IsTimedOut = true } ;
}
_logger . Info ( "HttpClientManager.GET 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 ;
return new HttpResponseInfo
{
Content = memoryStream ,
StatusCode = httpResponse . StatusCode ,
ContentType = httpResponse . ContentType
} ;
}
}
}
catch ( OperationCanceledException ex )
{
var exception = GetCancellationException ( options . Url , options . CancellationToken , ex ) ;
var httpException = exception as HttpException ;
if ( httpException ! = null & & httpException . IsTimedOut )
{
client . LastTimeout = DateTime . UtcNow ;
}
throw exception ;
}
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>
@ -318,37 +220,54 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
}
}
/// <summary>
/// <summary>
/// Performs a POST request
/// send as an asynchronous operation.
/// </summary>
/// </summary>
/// <param name="options">The options.</param>
/// <param name="options">The options.</param>
/// <param name=" postData">Params to add to the POST data .</param>
/// <param name=" httpMethod">The HTTP method .</param>
/// <returns> stream on success, null on failure </returns>
/// <returns> Task{HttpResponseInfo}. </returns>
/// <exception cref="HttpException">
/// <exception cref="HttpException">
/// </exception>
/// </exception>
/// <exception cref="System.ArgumentNullException">postData</exception>
private async Task < HttpResponseInfo > SendAsync ( HttpRequestOptions options , string httpMethod )
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task < Stream > Post ( HttpRequestOptions options , Dictionary < string , string > postData )
{
{
ValidateParams ( options .Url , options . CancellationToken );
ValidateParams ( options ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
options . CancellationToken . ThrowIfCancellationRequested ( ) ;
var httpWebRequest = GetRequest ( options , "POST" , options . EnableHttpCompression ) ;
var client = GetHttpClient ( GetHostFromUrl ( options . Url ) , options . EnableHttpCompression ) ;
if ( ( DateTime . UtcNow - client . LastTimeout ) . TotalSeconds < TimeoutSeconds )
{
throw new HttpException ( string . Format ( "Cancelling connection to {0} due to a previous timeout." , options . Url ) ) { IsTimedOut = true } ;
}
var strings = postData . Keys . Select ( key = > string . Format ( "{0}={1}" , key , postData [ key ] ) ) ;
var httpWebRequest = GetRequest ( options , httpMethod , options . EnableHttpCompression ) ;
var postContent = string . Join ( "&" , strings . ToArray ( ) ) ;
var bytes = Encoding . UTF8 . GetBytes ( postContent ) ;
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 = "application/x-www-form-urlencoded" ;
httpWebRequest . ContentType = options . RequestContentType ? ? "application/x-www-form-urlencoded" ;
httpWebRequest . ContentLength = bytes . Length ;
httpWebRequest . ContentLength = bytes . Length ;
httpWebRequest . GetRequestStream ( ) . Write ( bytes , 0 , bytes . Length ) ;
httpWebRequest . GetRequestStream ( ) . Write ( bytes , 0 , bytes . Length ) ;
}
if ( options . ResourcePool ! = null )
if ( options . ResourcePool ! = null )
{
{
await options . ResourcePool . WaitAsync ( options . CancellationToken ) . ConfigureAwait ( false ) ;
await options . ResourcePool . WaitAsync ( options . CancellationToken ) . ConfigureAwait ( false ) ;
}
}
_logger . Info ( "HttpClientManager.POST url: {0}" , options . Url ) ;
if ( ( DateTime . UtcNow - client . LastTimeout ) . TotalSeconds < TimeoutSeconds )
{
if ( options . ResourcePool ! = null )
{
options . ResourcePool . Release ( ) ;
}
throw new HttpException ( string . Format ( "Connection to {0} timed out" , options . Url ) ) { IsTimedOut = true } ;
}
_logger . Info ( "HttpClientManager {0}: {1}" , httpMethod . ToUpper ( ) , options . Url ) ;
try
try
{
{
@ -370,7 +289,14 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
memoryStream . Position = 0 ;
memoryStream . Position = 0 ;
return memoryStream ;
return new HttpResponseInfo
{
Content = memoryStream ,
StatusCode = httpResponse . StatusCode ,
ContentType = httpResponse . ContentType
} ;
}
}
}
}
}
}
@ -378,6 +304,13 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{
{
var exception = GetCancellationException ( options . Url , options . CancellationToken , ex ) ;
var exception = GetCancellationException ( options . Url , options . CancellationToken , ex ) ;
var httpException = exception as HttpException ;
if ( httpException ! = null & & httpException . IsTimedOut )
{
client . LastTimeout = DateTime . UtcNow ;
}
throw exception ;
throw exception ;
}
}
catch ( HttpRequestException ex )
catch ( HttpRequestException ex )
@ -407,6 +340,34 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
}
}
}
}
public Task < HttpResponseInfo > Post ( HttpRequestOptions options )
{
return SendAsync ( options , "POST" ) ;
}
/// <summary>
/// Performs a POST request
/// </summary>
/// <param name="options">The options.</param>
/// <param name="postData">Params to add to the POST data.</param>
/// <returns>stream on success, null on failure</returns>
/// <exception cref="HttpException">
/// </exception>
/// <exception cref="System.ArgumentNullException">postData</exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task < Stream > Post ( HttpRequestOptions options , Dictionary < string , string > postData )
{
var strings = postData . Keys . Select ( key = > string . Format ( "{0}={1}" , key , postData [ key ] ) ) ;
var postContent = string . Join ( "&" , strings . ToArray ( ) ) ;
options . RequestContent = postContent ;
options . RequestContentType = "application/x-www-form-urlencoded" ;
var response = await Post ( options ) . ConfigureAwait ( false ) ;
return response . Content ;
}
/// <summary>
/// <summary>
/// Performs a POST request
/// Performs a POST request
/// </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 ) ;
@ -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" ) ;
}
}
}
}