private async Task <HttpResponse> PostPutInternal(string method)
        {
            NetCore11HttpRequest request = this.ConfigureWebRequest(method, this.Url);

            this.PreparePostBody(request.RequestMessage);

            this.WriteRequestBody(request);
            return(await this.GetResponse(request));
        }
        private async Task <HttpResponse> GetStyleMethodInternal(string method)
        {
            NetCore11HttpRequest request = this.ConfigureWebRequest(method, this.Url);

            if (this.HasBody && (method == "DELETE" || method == "OPTIONS"))
            {
                request.RequestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(this.RequestContentType);
                this.WriteRequestBody(request);
            }

            return(await this.GetResponse(request));
        }
        private NetCore11HttpRequest ConfigureWebRequest(string method, Uri url)
        {
            var request = new NetCore11HttpRequest(method, url);

            ////////// 1. Configure HttpRequestMessage.
            this.AppendHeaders(request.RequestMessage);

            // Override certain headers that are controlled by RestSharp.
            request.RequestMessage.Headers.TE.Clear();
            request.RequestMessage.Headers.TE.Add(new TransferCodingWithQualityHeaderValue("deflate"));
            request.RequestMessage.Headers.TE.Add(new TransferCodingWithQualityHeaderValue("gzip"));
            request.RequestMessage.Headers.TE.Add(new TransferCodingWithQualityHeaderValue("identity"));

            if (this.UserAgent.HasValue())
            {
                request.RequestMessage.Headers.UserAgent.Add(ProductInfoHeaderValue.Parse(this.UserAgent));
            }

            ////////// 2. Configure HttpClientHandler
            this.AppendCookies(request.RequestHandler, url);

            // This matches with request.Headers.TE header.
            request.RequestHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;

            // Handler credentials.
            request.RequestHandler.UseDefaultCredentials = this.UseDefaultCredentials;
            if (this.Credentials != null)
            {
                request.RequestHandler.Credentials = this.Credentials;
            }

            // redirects
            request.RequestHandler.AllowAutoRedirect = this.FollowRedirects;
            if (this.FollowRedirects && this.MaxRedirects.HasValue)
            {
                request.RequestHandler.MaxAutomaticRedirections = this.MaxRedirects.Value;
            }

            ////////// 3. Configure HttpClient
            if (this.Timeout != 0)
            {
                request.RequestClient.Timeout = TimeSpan.FromMilliseconds(this.Timeout);
            }

            return(request);
        }
        private async Task <HttpResponse> GetResponse(NetCore11HttpRequest request)
        {
            HttpResponse response = new HttpResponse {
                ResponseStatus = ResponseStatus.None
            };

            try
            {
                NetCore11HttpResponse webResponse = await GetRawResponse(request);

                this.ExtractResponseData(response, webResponse);
            }
            catch (Exception ex)
            {
                this.ExtractErrorResponse(response, ex);
            }

            return(response);
        }
        private void WriteRequestBody(NetCore11HttpRequest webRequest)
        {
            if (this.HasBody || this.HasFiles || this.AlwaysMultipartFormData)
            {
                webRequest.RequestMessage.Content.Headers.ContentLength = this.CalculateContentLength();
            }

            using (Stream requestStream = webRequest.GetRequestStreamAsync())
            {
                if (this.HasFiles || this.AlwaysMultipartFormData)
                {
                    this.WriteMultipartFormData(requestStream);
                }
                else if (this.RequestBodyBytes != null)
                {
                    requestStream.Write(this.RequestBodyBytes, 0, this.RequestBodyBytes.Length);
                }
                else if (this.RequestBody != null)
                {
                    this.WriteStringTo(requestStream, this.RequestBody);
                }
            }
        }
        private static async Task <NetCore11HttpResponse> GetRawResponse(NetCore11HttpRequest request)
        {
            try
            {
                return(await request.GetResponseAsync());
            }
            catch (WebException ex)
            {
                // Check to see if this is an HTTP error or a transport error.
                // In cases where an HTTP error occurs ( status code >= 400 )
                // return the underlying HTTP response, otherwise assume a
                // transport exception (ex: connection timeout) and
                // rethrow the exception

                var exResponse = ex.Response as NetCore11HttpResponse;
                if (exResponse != null)
                {
                    return(exResponse);
                }

                throw;
            }
        }