Exemple #1
0
        public static async Task <string> GetResponseTextByProxyAsync(Uri uri, IWebProxySelector proxySelector, Func <Uri, IWebProxy, Task <string> > rspTextFetcher, byte retryTimes = 9)
        {
            string rspText = null;

            if (retryTimes < 4)
            {
                retryTimes = 4;
            }
            bool isSuccessfulByNormalProxies = false;

            for (byte i = 0; i < retryTimes - 1; i++)
            {
                var proxy = proxySelector.SelectNextProxy();
                rspText = await rspTextFetcher.Invoke(uri, proxy);

                if (!string.IsNullOrEmpty(rspText))
                {
                    isSuccessfulByNormalProxies = true;
                    proxySelector.OnNormalProxySuccess(proxy);
                    break;
                }
                proxySelector.OnNormalProxyFail(proxy);
            }
            if (!isSuccessfulByNormalProxies)
            {
                for (byte i = 0; i < 2; i++)
                {
                    bool isAdvancedProxy = proxySelector.TryPreferAdvancedProxy(out var proxy);
                    rspText = await rspTextFetcher.Invoke(uri, proxy);

                    if (!string.IsNullOrEmpty(rspText))
                    {
                        if (isAdvancedProxy)
                        {
                            proxySelector.OnAdvancedProxySuccess(proxy);
                        }
                        else
                        {
                            proxySelector.OnNormalProxySuccess(proxy);
                        }
                        break;
                    }
                    if (isAdvancedProxy)
                    {
                        proxySelector.OnAdvancedProxyFail(proxy);
                    }
                    else
                    {
                        proxySelector.OnNormalProxyFail(proxy);
                    }
                }
            }
            return(rspText);
        }
Exemple #2
0
        public static async Task <string> GetResponseTextByProxyAsync(Uri uri, IWebProxySelector proxySelector, Func <Uri, IWebProxy, Task <HttpResponseMessage> > rspFetcher, Predicate <string> rspValidator = null, byte retryTimes = 9)
        {
            string rspText = null;

            if (retryTimes < 4)
            {
                retryTimes = 4;
            }
            bool isSuccessfulByNormalProxies = false;

            for (byte i = 0; i < retryTimes - 1; i++)
            {
                var proxy = proxySelector.SelectNextProxy();
                rspText = await GetResponseTextByProxyAsync(uri, rspFetcher, proxy, rspValidator);

                if (rspText != null)
                {
                    isSuccessfulByNormalProxies = true;
                    proxySelector.OnNormalProxySuccess(proxy);
                    break;
                }
                proxySelector.OnNormalProxyFail(proxy);
            }
            if (!isSuccessfulByNormalProxies)
            {
                for (byte i = 0; i < 2; i++)
                {
                    bool isAdvancedProxy = proxySelector.TryPreferAdvancedProxy(out var proxy);
                    rspText = await GetResponseTextByProxyAsync(uri, rspFetcher, proxy, rspValidator);

                    if (rspText != null)
                    {
                        if (isAdvancedProxy)
                        {
                            proxySelector.OnAdvancedProxySuccess(proxy);
                        }
                        else
                        {
                            proxySelector.OnNormalProxySuccess(proxy);
                        }
                        break;
                    }
                    if (isAdvancedProxy)
                    {
                        proxySelector.OnAdvancedProxyFail(proxy);
                    }
                    else
                    {
                        proxySelector.OnNormalProxyFail(proxy);
                    }
                }
            }
            return(rspText);
        }