Пример #1
0
        /// <summary>
        /// Returns the HTTP response.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task <IWebResponseData> GetResponseAsync(System.Threading.CancellationToken cancellationToken)
        {
            try
            {
                var responseMessage = await _httpClient.SendAsync(_request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                                      .ConfigureAwait(continueOnCapturedContext: false);

                bool disposeClient = ClientConfig.DisposeHttpClients(_clientConfig);
                // If AllowAutoRedirect is set to false, HTTP 3xx responses are returned back as response.
                if (!_clientConfig.AllowAutoRedirect &&
                    responseMessage.StatusCode >= HttpStatusCode.Ambiguous &&
                    responseMessage.StatusCode < HttpStatusCode.BadRequest)
                {
                    return(new HttpClientResponseData(responseMessage, _httpClient, disposeClient));
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    // For all responses other than HTTP 2xx, return an exception.
                    throw new Amazon.Runtime.Internal.HttpErrorResponseException(
                              new HttpClientResponseData(responseMessage, _httpClient, disposeClient));
                }

                return(new HttpClientResponseData(responseMessage, _httpClient, disposeClient));
            }
            catch (HttpRequestException httpException)
            {
                if (httpException.InnerException != null)
                {
                    if (httpException.InnerException is IOException)
                    {
                        throw httpException.InnerException;
                    }
#if !NETSTANDARD
                    if (httpException.InnerException is WebException)
                    {
                        throw httpException.InnerException;
                    }
#endif
                }

                throw;
            }
            catch (OperationCanceledException canceledException)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    //OperationCanceledException thrown by HttpClient not the CancellationToken supplied by the user.
                    //This exception can wrap at least IOExceptions, ObjectDisposedExceptions and should be retried.
                    //Throw the underlying exception if it exists.
                    if (canceledException.InnerException != null)
                    {
                        throw canceledException.InnerException;
                    }
                }

                throw;
            }
        }
        /// <summary>
        /// Returns the HTTP response.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task <IWebResponseData> GetResponseAsync(System.Threading.CancellationToken cancellationToken)
        {
            try
            {
                var responseMessage = await _httpClient.SendAsync(_request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                                      .ConfigureAwait(continueOnCapturedContext: false);

                bool disposeClient = ClientConfig.DisposeHttpClients(_clientConfig);
                // If AllowAutoRedirect is set to false, HTTP 3xx responses are returned back as response.
                if (!_clientConfig.AllowAutoRedirect &&
                    responseMessage.StatusCode >= HttpStatusCode.Ambiguous &&
                    responseMessage.StatusCode < HttpStatusCode.BadRequest)
                {
                    return(new HttpClientResponseData(responseMessage, _httpClient, disposeClient));
                }

                if (!responseMessage.IsSuccessStatusCode)
                {
                    // For all responses other than HTTP 2xx, return an exception.
                    throw new Amazon.Runtime.Internal.HttpErrorResponseException(
                              new HttpClientResponseData(responseMessage, _httpClient, disposeClient));
                }

                return(new HttpClientResponseData(responseMessage, _httpClient, disposeClient));
            }
            catch (HttpRequestException e)
            {
                if (e.InnerException != null)
                {
                    if (e.InnerException is IOException)
                    {
                        throw e.InnerException;
                    }
#if !NETSTANDARD
                    if (e.InnerException is WebException)
                    {
                        throw e.InnerException;
                    }
#endif
                }

                throw;
            }
        }