Ejemplo n.º 1
0
        private async Task <bool> CheckProxy(string proxyUrl)
        {
            var uri     = new Uri(_baseUrl);
            var builder = new UriBuilder(uri)
            {
                Path = String.Empty
            };

            uri = builder.Uri;

            var handler = new HttpClientHandler {
                Proxy = new WebProxy(new Uri(proxyUrl)), AllowAutoRedirect = true
            };
            var client = new HttpClient(handler)
            {
                Timeout = TimeSpan.FromSeconds(_requestTimeout)
            };

            try
            {
                var req = new HttpRequestMessage(HttpMethod.Head, uri);
                req.Headers.UserAgent.ParseAdd(UserAgentSelector.GetRandomAgent());

                var resp = await client.SendAsync(req);

                return(resp.IsSuccessStatusCode);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        private async Task InitializeHttpClient()
        {
            var userAgent   = UserAgentSelector.GetRandomAgent();
            var httpHandler = new HttpClientHandler();
            var httpClient  = new HttpClient(httpHandler)
            {
                BaseAddress = new Uri(_baseUrl),
                Timeout     = TimeSpan.FromSeconds(_requestTimeout)
            };

            if (_proxyProvider != null)
            {
                var proxyUrl = await _proxyProvider.GetProxyAsync(CheckProxy);

                if (!String.IsNullOrEmpty(proxyUrl))
                {
                    httpHandler.Proxy = new WebProxy(new Uri(proxyUrl));
                    _logger.Info("UzService uses proxy " + proxyUrl);
                }
                else
                {
                    _logger.Warning("Cannot obtain proxy, requesting unproxied");
                }
            }

            _httpClient  = httpClient;
            _httpHandler = httpHandler;
            _userAgent   = userAgent;
        }