Example #1
0
 public Connection(Uri url, bool insecure)
 {
     if (insecure)
     {
         client = insecureClients.GetOrAdd(url, _ => new HttpClient(insecureHandler)
         {
             BaseAddress = url,
             Timeout     = TimeSpan.FromMilliseconds(FibSystemProperties.GetHttpTimeout()),
         });
     }
     else
     {
         client = clients.GetOrAdd(url, _ => new HttpClient
         {
             BaseAddress = url,
             Timeout     = TimeSpan.FromMilliseconds(FibSystemProperties.GetHttpTimeout()),
         });
     }
 }
Example #2
0
        public Connection(Uri url, bool insecure, IEventHandlers eventHandlers = null)
        {
            _eventHandlers = eventHandlers;
            var      proxy  = FibSystemProperties.GetHttpProxy();
            WebProxy proxy1 = null;

            if (!string.IsNullOrEmpty(proxy))
            {
                proxyLog = $"use proxy:{proxy}";
                if (proxy.Contains("@_@"))
                {
                    //127.0.0.1:8080@_@username&pass
                    var arr = proxy.Split(new string[] { "@_@" }, StringSplitOptions.None);
                    proxy1 = new WebProxy
                    {
                        Address               = new Uri($"http://{arr}"),
                        BypassProxyOnLocal    = false,
                        UseDefaultCredentials = false,

                        // *** These creds are given to the proxy server, not the web server ***
                        Credentials = new NetworkCredential(
                            userName: arr[1].Split('&')[0],
                            password: arr[1].Split('&')[1])
                    };
                }
                else
                {
                    proxy1 = new WebProxy
                    {
                        Address               = new Uri($"http://{proxy}"),
                        BypassProxyOnLocal    = false,
                        UseDefaultCredentials = false,
                    };
                }
            }



            if (insecure)
            {
                if (proxy1 != null)
                {
                    client = insecureClients.GetOrAdd(url, _ =>
                    {
                        var httpClientHandler = new HttpClientHandler
                        {
                            Proxy = proxy1, ServerCertificateCustomValidationCallback = (_____, __, ___, ____) => true
                        };
                        var c = new HttpClient(httpClientHandler)
                        {
                            BaseAddress = url,
                            Timeout     = TimeSpan.FromMilliseconds(FibSystemProperties.GetHttpTimeout()),
                        };
                        return(c);
                    });
                }
                else
                {
                    client = insecureClients.GetOrAdd(url, _ => new HttpClient(insecureHandler)
                    {
                        BaseAddress = url,
                        Timeout     = TimeSpan.FromMilliseconds(FibSystemProperties.GetHttpTimeout()),
                    });
                }
            }
            else
            {
                if (proxy1 != null)
                {
                    client = clients.GetOrAdd(url, _ =>
                    {
                        var httpClientHandler = new HttpClientHandler
                        {
                            Proxy = proxy1,
                        };
                        var c = new HttpClient(httpClientHandler)
                        {
                            BaseAddress = url,
                            Timeout     = TimeSpan.FromMilliseconds(FibSystemProperties.GetHttpTimeout()),
                        };
                        return(c);
                    });
                }
                else
                {
                    client = clients.GetOrAdd(url, _ => new HttpClient
                    {
                        BaseAddress = url,
                        Timeout     = TimeSpan.FromMilliseconds(FibSystemProperties.GetHttpTimeout()),
                    });
                }
            }
        }