private void EnsureSuccessStatusCode(HttpClientInfo client, HttpWebResponse response, HttpRequestOptions options) { var statusCode = response.StatusCode; var isSuccessful = statusCode >= HttpStatusCode.OK && statusCode <= (HttpStatusCode)299; if (!isSuccessful) { if (options.LogErrorResponseBody) { try { using (var stream = response.GetResponseStream()) { if (stream != null) { using (var reader = new StreamReader(stream)) { var msg = reader.ReadToEnd(); } } } } catch { } } throw new HttpException(response.StatusDescription) { StatusCode = response.StatusCode }; } }
private Exception GetException(Exception ex, HttpRequestOptions options, HttpClientInfo client) { if (ex is HttpException) { return(ex); } var webException = ex as WebException ?? ex.InnerException as WebException; if (webException != null) { if (options.LogErrors) { } var exception = new HttpException(ex.Message, ex); var response = webException.Response as HttpWebResponse; if (response != null) { exception.StatusCode = response.StatusCode; if ((int)response.StatusCode == 429) { client.LastTimeout = DateTime.UtcNow; } } return(exception); } var operationCanceledException = ex as OperationCanceledException ?? ex.InnerException as OperationCanceledException; if (operationCanceledException != null) { return(GetCancellationException(options, client, options.CancellationToken, operationCanceledException)); } if (options.LogErrors) { } return(ex); }
/// <summary> /// Gets /// </summary> /// <param name="host">The host.</param> /// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param> /// <returns>HttpClient.</returns> /// <exception cref="System.ArgumentNullException">host</exception> private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression) { if (string.IsNullOrEmpty(host)) { throw new ArgumentNullException("host"); } HttpClientInfo client; var key = host + enableHttpCompression; if (!_httpClients.TryGetValue(key, out client)) { client = new HttpClientInfo(); _httpClients.TryAdd(key, client); } return client; }
/// <summary> /// Gets /// </summary> /// <param name="host">The host.</param> /// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param> /// <returns>HttpClient.</returns> /// <exception cref="System.ArgumentNullException">host</exception> private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression) { if (string.IsNullOrEmpty(host)) { throw new ArgumentNullException("host"); } HttpClientInfo client; var key = host + enableHttpCompression; if (!_httpClients.TryGetValue(key, out client)) { client = new HttpClientInfo(); _httpClients.TryAdd(key, client); } return(client); }
/// <summary> /// Throws the cancellation exception. /// </summary> /// <param name="options">The options.</param> /// <param name="client">The client.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="exception">The exception.</param> /// <returns>Exception.</returns> private Exception GetCancellationException(HttpRequestOptions options, HttpClientInfo client, CancellationToken cancellationToken, OperationCanceledException exception) { // If the HttpClient's timeout is reached, it will cancel the Task internally if (!cancellationToken.IsCancellationRequested) { var msg = string.Format("Connection to {0} timed out", options.Url); if (options.LogErrors) { } client.LastTimeout = DateTime.UtcNow; // Throw an HttpException so that the caller doesn't think it was cancelled by user code return(new HttpException(msg, exception) { IsTimedOut = true }); } return(exception); }
/// <summary> /// Throws the cancellation exception. /// </summary> /// <param name="options">The options.</param> /// <param name="client">The client.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="exception">The exception.</param> /// <returns>Exception.</returns> private Exception GetCancellationException(HttpRequestOptions options, HttpClientInfo client, CancellationToken cancellationToken, OperationCanceledException exception) { // If the HttpClient's timeout is reached, it will cancel the Task internally if (!cancellationToken.IsCancellationRequested) { var msg = string.Format("Connection to {0} timed out", options.Url); if (options.LogErrors) { } client.LastTimeout = DateTime.UtcNow; // Throw an HttpException so that the caller doesn't think it was cancelled by user code return new HttpException(msg, exception) { IsTimedOut = true }; } return exception; }
private Exception GetException(Exception ex, HttpRequestOptions options, HttpClientInfo client) { if (ex is HttpException) { return ex; } var webException = ex as WebException ?? ex.InnerException as WebException; if (webException != null) { if (options.LogErrors) { } var exception = new HttpException(ex.Message, ex); var response = webException.Response as HttpWebResponse; if (response != null) { exception.StatusCode = response.StatusCode; if ((int)response.StatusCode == 429) { client.LastTimeout = DateTime.UtcNow; } } return exception; } var operationCanceledException = ex as OperationCanceledException ?? ex.InnerException as OperationCanceledException; if (operationCanceledException != null) { return GetCancellationException(options, client, options.CancellationToken, operationCanceledException); } if (options.LogErrors) { } return ex; }