Esempio n. 1
0
        protected async Task ProcessResponseAsync(HttpResponseMessage response)
        {
            var message = $"Server responded with status code {(int)response.StatusCode} - {response.ReasonPhrase}";

            if (!string.IsNullOrWhiteSpace(this.ErrorStatusCodes))
            {
                var errorCodeRanges = StatusCodeRangeList.Parse(this.ErrorStatusCodes);
                if (errorCodeRanges.IsInAnyRange((int)response.StatusCode))
                {
                    this.LogError(message);
                }
                else
                {
                    this.LogInformation(message);
                }
            }
            else
            {
                if (response.IsSuccessStatusCode)
                {
                    this.LogInformation(message);
                }
                else
                {
                    this.LogError(message);
                }
            }

            using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
            {
                var text = await this.GetTruncatedResponseAsync(responseStream).ConfigureAwait(false);

                if (string.IsNullOrEmpty(text))
                {
                    this.LogDebug("The Content Length of the response was 0.");
                }
                else
                {
                    this.ResponseBodyVariable = text;
                    if (this.LogResponseBody)
                    {
                        if (text.Length >= this.MaxResponseLength)
                        {
                            this.LogDebug($"The following response Content Body is truncated to {this.MaxResponseLength} characters.");
                        }

                        this.LogInformation("Response Content Body: " + text);
                    }
                }
            }
        }
Esempio n. 2
0
        private async Task PerformRequestAsync(Stream fileStream, CancellationToken cancellationToken)
        {
            using (var client = this.CreateClient())
            {
                using (var response = await client.GetAsync(this.Url, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
                {
                    var message = $"Server responded with status code {(int)response.StatusCode} - {response.ReasonPhrase}";
                    if (!string.IsNullOrWhiteSpace(this.ErrorStatusCodes))
                    {
                        var errorCodeRanges = StatusCodeRangeList.Parse(this.ErrorStatusCodes);
                        if (errorCodeRanges.IsInAnyRange((int)response.StatusCode))
                        {
                            this.LogError(message);
                        }
                        else
                        {
                            this.LogInformation(message);
                        }
                    }
                    else
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            this.LogInformation(message);
                        }
                        else
                        {
                            this.LogError(message);
                        }
                    }

                    this.TotalSize = response.Content.Headers.ContentLength ?? 0;

                    using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    {
                        await responseStream.CopyToAsync(fileStream, 4096, cancellationToken, pos => this.CurrentPosition = pos).ConfigureAwait(false);
                    }
                }
            }
        }