예제 #1
0
        /// <summary>
        ///     Apply settings on the WebRequestHandler, this also calls the SetDefaults for the underlying HttpClientHandler
        /// </summary>
        /// <param name="webRequestHandler">WebRequestHandler to set the defaults to</param>
        private static void SetDefaults(WebRequestHandler webRequestHandler)
        {
            var httpBehaviour = HttpBehaviour.Current;

            SetDefaults(webRequestHandler as HttpClientHandler);

            var httpSettings = httpBehaviour.HttpSettings ?? HttpExtensionsGlobals.HttpSettings;

            webRequestHandler.AllowPipelining          = httpSettings.AllowPipelining;
            webRequestHandler.AuthenticationLevel      = httpSettings.AuthenticationLevel;
            webRequestHandler.CachePolicy              = new RequestCachePolicy(httpSettings.RequestCacheLevel);
            webRequestHandler.ClientCertificateOptions = httpSettings.ClientCertificateOptions;
            // Add certificates, if any
            if (httpSettings.ClientCertificates?.Count > 0)
            {
                webRequestHandler.ClientCertificates.AddRange(httpSettings.ClientCertificates);
            }
            webRequestHandler.ContinueTimeout          = httpSettings.ContinueTimeout;
            webRequestHandler.ImpersonationLevel       = httpSettings.ImpersonationLevel;
            webRequestHandler.MaxResponseHeadersLength = httpSettings.MaxResponseHeadersLength;
            webRequestHandler.Proxy            = httpSettings.UseProxy ? WebProxyFactory.Create() : null;
            webRequestHandler.ReadWriteTimeout = httpSettings.ReadWriteTimeout;

            // Add logic to ignore the certificate
            if (httpSettings.IgnoreSslCertificateErrors)
            {
                webRequestHandler.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
                {
                    if (sslPolicyErrors != SslPolicyErrors.None)
                    {
                        Log.Warn().WriteLine("Ssl policy error {0}", sslPolicyErrors);
                    }
                    return(true);
                };
            }
        }
예제 #2
0
        /// <summary>
        ///     This creates an advanced HttpMessageHandler, used in Apps
        /// </summary>
        /// <returns>HttpMessageHandler (HttpClientHandler)</returns>
        private static HttpMessageHandler CreateHandler()
        {
#if !NET461
            var httpClientHandler = new HttpClientHandler();
#else
            var httpClientHandler = new WebRequestHandler();
#endif
            var httpBehaviour = HttpBehaviour.Current;
            var httpSettings  = httpBehaviour.HttpSettings ?? HttpExtensionsGlobals.HttpSettings;

#if NET461
            httpClientHandler.AllowPipelining     = httpSettings.AllowPipelining;
            httpClientHandler.AuthenticationLevel = httpSettings.AuthenticationLevel;
            httpClientHandler.ContinueTimeout     = httpSettings.ContinueTimeout;
            httpClientHandler.ImpersonationLevel  = httpSettings.ImpersonationLevel;
            httpClientHandler.ReadWriteTimeout    = httpSettings.ReadWriteTimeout;
            httpClientHandler.CachePolicy         = new RequestCachePolicy(httpSettings.RequestCacheLevel);
#else
            httpClientHandler.MaxConnectionsPerServer = httpSettings.MaxConnectionsPerServer;
#endif

            httpClientHandler.AutomaticDecompression = httpSettings.DefaultDecompressionMethods;

            httpClientHandler.AllowAutoRedirect        = httpSettings.AllowAutoRedirect;
            httpClientHandler.AutomaticDecompression   = httpSettings.DefaultDecompressionMethods;
            httpClientHandler.ClientCertificateOptions = httpSettings.ClientCertificateOptions;
            // Add certificates, if any
            if (httpSettings.ClientCertificates?.Count > 0)
            {
                httpClientHandler.ClientCertificates.AddRange(httpSettings.ClientCertificates);
            }
            httpClientHandler.Credentials = httpSettings.UseDefaultCredentials ? CredentialCache.DefaultCredentials : httpSettings.Credentials;
            httpClientHandler.MaxAutomaticRedirections    = httpSettings.MaxAutomaticRedirections;
            httpClientHandler.MaxRequestContentBufferSize = httpSettings.MaxRequestContentBufferSize;
            httpClientHandler.MaxResponseHeadersLength    = httpSettings.MaxResponseHeadersLength;
            httpClientHandler.UseCookies            = httpSettings.UseCookies;
            httpClientHandler.CookieContainer       = httpSettings.UseCookies ? httpBehaviour.CookieContainer : null;
            httpClientHandler.UseDefaultCredentials = httpSettings.UseDefaultCredentials;
            httpClientHandler.PreAuthenticate       = httpSettings.PreAuthenticate;

#if !NETSTANDARD1_3
            httpClientHandler.Proxy = httpSettings.UseProxy ? WebProxyFactory.Create() : null;
#endif
            httpClientHandler.UseProxy = httpSettings.UseProxy;


            // Add logic to ignore the certificate
            if (httpSettings.IgnoreSslCertificateErrors)
            {
#if !NET461
                httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
#else
                httpClientHandler.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
#endif
                {
                    if (sslPolicyErrors != SslPolicyErrors.None)
                    {
                        Log.Warn().WriteLine("Ssl policy error {0}", sslPolicyErrors);
                    }
                    return(true);
                };
            }
            return(httpClientHandler);
        }