protected virtual HttpWebRequest ConfigureWebRequest(string method, Uri url)
        {
            var webRequest = CreateWebRequest(url);

            webRequest.UseDefaultCredentials = UseDefaultCredentials;

            webRequest.PreAuthenticate = PreAuthenticate;
            webRequest.Pipelined       = Pipelined;
            webRequest.UnsafeAuthenticatedConnectionSharing = UnsafeAuthenticatedConnectionSharing;
#if NETSTANDARD2_0
            webRequest.Proxy = null;
#endif
            try
            {
                webRequest.ServicePoint.Expect100Continue = false;
            }
            catch (PlatformNotSupportedException)
            {
                // Avoid to crash in UWP apps
            }

            AppendHeaders(webRequest);
            AppendCookies(webRequest);

            if (Host != null)
            {
                webRequest.Host = Host;
            }

            webRequest.Method = method;

            // make sure Content-Length header is always sent since default is -1
            if (!HasFiles && !AlwaysMultipartFormData && method != "GET")
            {
                webRequest.ContentLength = 0;
            }

            if (Credentials != null)
            {
                webRequest.Credentials = Credentials;
            }

            if (UserAgent.HasValue())
            {
                webRequest.UserAgent = UserAgent;
            }

            if (ClientCertificates != null)
            {
                webRequest.ClientCertificates.AddRange(ClientCertificates);
            }

            AllowedDecompressionMethods.ForEach(x => { webRequest.AutomaticDecompression |= x; });

            if (AutomaticDecompression)
            {
                webRequest.AutomaticDecompression =
                    DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
            }

            if (Timeout != 0)
            {
                webRequest.Timeout = Timeout;
            }

            if (ReadWriteTimeout != 0)
            {
                webRequest.ReadWriteTimeout = ReadWriteTimeout;
            }

            webRequest.Proxy = Proxy;

            if (CachePolicy != null)
            {
                webRequest.CachePolicy = CachePolicy;
            }

            webRequest.AllowAutoRedirect = FollowRedirects;

            if (FollowRedirects && MaxRedirects.HasValue)
            {
                webRequest.MaximumAutomaticRedirections = MaxRedirects.Value;
            }

            webRequest.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;

            webRequest.ConnectionGroupName = ConnectionGroupName;

            if (WebRequestConfigurator != null)
            {
                WebRequestConfigurator.Invoke(webRequest);
            }

            return(webRequest);
        }
Ejemplo n.º 2
0
        // TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code
        // TODO: duplication at the moment).
        private HttpWebRequest ConfigureWebRequest(string method, Uri url)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.UseDefaultCredentials          = this.UseDefaultCredentials;
            webRequest.PreAuthenticate                = this.PreAuthenticate;
            webRequest.ServicePoint.Expect100Continue = false;

            this.AppendHeaders(webRequest);
            this.AppendCookies(webRequest);

            webRequest.Method = method;

            // make sure Content-Length header is always sent since default is -1
            if (!this.HasFiles && !this.AlwaysMultipartFormData)
            {
                webRequest.ContentLength = 0;
            }

            // webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
            // Fabrizio
            AllowedDecompressionMethods.ForEach(x => { webRequest.AutomaticDecompression |= x; });

#if FRAMEWORK
            if (this.ClientCertificates != null)
            {
                webRequest.ClientCertificates.AddRange(this.ClientCertificates);
            }
#endif

            if (this.UserAgent.HasValue())
            {
                webRequest.UserAgent = this.UserAgent;
            }

            if (this.Timeout != 0)
            {
                webRequest.Timeout = this.Timeout;
            }

            if (this.ReadWriteTimeout != 0)
            {
                webRequest.ReadWriteTimeout = this.ReadWriteTimeout;
            }

            if (this.Credentials != null)
            {
                webRequest.Credentials = this.Credentials;
            }

            if (this.Proxy != null)
            {
                webRequest.Proxy = this.Proxy;
            }

#if FRAMEWORK
            if (this.CachePolicy != null)
            {
                webRequest.CachePolicy = this.CachePolicy;
            }
#endif

            webRequest.AllowAutoRedirect = this.FollowRedirects;

            if (this.FollowRedirects && this.MaxRedirects.HasValue)
            {
                webRequest.MaximumAutomaticRedirections = this.MaxRedirects.Value;
            }

            return(webRequest);
        }