Exemple #1
0
        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();

                                    _logger.Error(msg);
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                throw new HttpException(response.StatusDescription)
                      {
                          StatusCode = response.StatusCode
                      };
            }
        }
Exemple #2
0
        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)
                {
                    _logger.LogError(webException, "Error {status} getting response from {url}", webException.Status, options.Url);
                }

                var exception = new HttpException(webException.Message, webException);

                using (var response = webException.Response as HttpWebResponse)
                {
                    if (response != null)
                    {
                        exception.StatusCode = response.StatusCode;

                        if ((int)response.StatusCode == 429)
                        {
                            client.LastTimeout = DateTime.UtcNow;
                        }
                    }
                }

                if (!exception.StatusCode.HasValue)
                {
                    if (webException.Status == WebExceptionStatus.NameResolutionFailure ||
                        webException.Status == WebExceptionStatus.ConnectFailure)
                    {
                        exception.IsTimedOut = true;
                    }
                }

                return(exception);
            }

            var operationCanceledException = ex as OperationCanceledException
                                             ?? ex.InnerException as OperationCanceledException;

            if (operationCanceledException != null)
            {
                return(GetCancellationException(options, client, options.CancellationToken, operationCanceledException));
            }

            if (options.LogErrors)
            {
                _logger.LogError(ex, "Error getting response from {url}", options.Url);
            }

            return(ex);
        }
Exemple #3
0
        /// <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="ArgumentNullException">host</exception>
        private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression)
        {
            if (string.IsNullOrEmpty(host))
            {
                throw new ArgumentNullException(nameof(host));
            }

            var key = host + enableHttpCompression;

            if (!_httpClients.TryGetValue(key, out var client))
            {
                client = new HttpClientInfo();

                _httpClients.TryAdd(key, client);
            }

            return(client);
        }
Exemple #4
0
        /// <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)
                {
                    _logger.LogError(msg);
                }

                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);
        }