private async ValueTask ProcessInternal(HttpMessage message, bool async)
        {
            var request = CreateRequest(message.Request);

            ServicePointHelpers.SetLimits(request.ServicePoint);

            using var registration = message.CancellationToken.Register(state => ((HttpWebRequest)state).Abort(), request);
            try
            {
                if (message.Request.Content != null)
                {
                    using var requestStream = async ? await request.GetRequestStreamAsync().ConfigureAwait(false) : request.GetRequestStream();

                    if (async)
                    {
                        await message.Request.Content.WriteToAsync(requestStream, message.CancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        message.Request.Content.WriteTo(requestStream, message.CancellationToken);
                    }
                }
                else
                {
                    // match the behavior of HttpClient
                    if (message.Request.Method != RequestMethod.Head &&
                        message.Request.Method != RequestMethod.Get &&
                        message.Request.Method != RequestMethod.Delete)
                    {
                        request.ContentLength = 0;
                    }

                    request.ContentType = null;
                }

                WebResponse webResponse;
                try
                {
                    webResponse = async ? await request.GetResponseAsync().ConfigureAwait(false) : request.GetResponse();
                }
                // HttpWebRequest throws for error responses catch that
                catch (WebException exception) when(exception.Response != null)
                {
                    webResponse = exception.Response;
                }

                message.Response = new HttpWebResponseImplementation(message.Request.ClientRequestId, (HttpWebResponse)webResponse);
            }
            // ObjectDisposedException might be thrown if the request is aborted during the content upload via SSL
            catch (ObjectDisposedException) when(message.CancellationToken.IsCancellationRequested)
            {
                CancellationHelper.ThrowIfCancellationRequested(message.CancellationToken);
            }
            catch (WebException webException)
            {
                // WebException is thrown in the case of .Abort() call
                CancellationHelper.ThrowIfCancellationRequested(message.CancellationToken);
                throw new RequestFailedException(0, webException.Message, webException);
            }
        }
Example #2
0
        private async ValueTask ProcessInternal(HttpMessage message, bool async)
        {
            var request = CreateRequest(message.Request);

            ServicePointHelpers.SetLimits(request.ServicePoint);

            using var registration = message.CancellationToken.Register(state => ((HttpWebRequest)state).Abort(), request);
            try
            {
                if (message.Request.Content != null)
                {
                    if (request.ContentLength == -1 &&
                        message.Request.Content.TryComputeLength(out var length))
                    {
                        request.ContentLength = length;
                    }

                    using var requestStream = async ? await request.GetRequestStreamAsync().ConfigureAwait(false) : request.GetRequestStream();

                    if (async)
                    {
                        await message.Request.Content.WriteToAsync(requestStream, message.CancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        message.Request.Content.WriteTo(requestStream, message.CancellationToken);
                    }
                }
                else
                {
                    request.ContentLength = 0;
                }

                WebResponse webResponse;
                try
                {
                    webResponse = async ? await request.GetResponseAsync().ConfigureAwait(false) : request.GetResponse();
                }
                // HttpWebRequest throws for error responses catch that
                catch (WebException exception) when(exception.Response != null)
                {
                    webResponse = exception.Response;
                }
                message.Response = new HttpWebResponseImplementation(message.Request.ClientRequestId, (HttpWebResponse)webResponse);
            }
            // WebException is thrown in the case of .Abort() call
            catch (WebException) when(message.CancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }
            catch (WebException webException)
            {
                throw new RequestFailedException(0, webException.Message);
            }
        }
Example #3
0
        private static HttpClient CreateDefaultClient()
        {
            var httpMessageHandler = CreateDefaultHandler();

            SetProxySettings(httpMessageHandler);
            ServicePointHelpers.SetLimits(httpMessageHandler);

            return(new HttpClient(httpMessageHandler)
            {
                // Timeouts are handled by the pipeline
                Timeout = Timeout.InfiniteTimeSpan
            });
        }
        private static HttpClient CreateDefaultClient(HttpPipelineTransportOptions?options = null)
        {
            var httpMessageHandler = CreateDefaultHandler(options);

            SetProxySettings(httpMessageHandler);
            ServicePointHelpers.SetLimits(httpMessageHandler);

            return(new HttpClient(httpMessageHandler)
            {
                // Timeouts are handled by the pipeline
                Timeout = Timeout.InfiniteTimeSpan,
            });
        }
        private static HttpClient CreateDefaultClient()
        {
            var httpClientHandler = new HttpClientHandler();

            if (HttpEnvironmentProxy.TryCreate(out IWebProxy webProxy))
            {
                httpClientHandler.Proxy = webProxy;
            }

#if NETFRAMEWORK
            ServicePointHelpers.SetLimits(httpClientHandler);
#endif

            return(new HttpClient(httpClientHandler));
        }
        private static HttpClient CreateDefaultClient()
        {
            var httpClientHandler = new HttpClientHandler();

            if (HttpEnvironmentProxy.TryCreate(out IWebProxy webProxy))
            {
                httpClientHandler.Proxy = webProxy;
            }

#if NETFRAMEWORK
            ServicePointHelpers.SetLimits(httpClientHandler);
#endif

            return(new HttpClient(httpClientHandler)
            {
                // Timeouts are handled by the pipeline
                Timeout = Timeout.InfiniteTimeSpan
            });
        }
        private static HttpClient CreateDefaultClient()
        {
#if NETFRAMEWORK || NETSTANDARD
            HttpClientHandler httpMessageHandler = new HttpClientHandler();
#else
            SocketsHttpHandler httpMessageHandler = new SocketsHttpHandler();
#endif
            if (HttpEnvironmentProxy.TryCreate(out IWebProxy webProxy))
            {
                httpMessageHandler.Proxy = webProxy;
            }

            ServicePointHelpers.SetLimits(httpMessageHandler);

            return(new HttpClient(httpMessageHandler)
            {
                // Timeouts are handled by the pipeline
                Timeout = Timeout.InfiniteTimeSpan
            });
        }