public void SavePageFetcherConfig(IPageFetcherConfig c)
        {
            var configSection = (TGFCSpidermanSection)_config.GetSection(SectionName);

            if (configSection == null)
            {
                configSection = new TGFCSpidermanSection
                {
                    PageFetcherElement =
                    {
                        SigninRetryTimes = c.SigninRetryTimes,
                        TimeoutInSeconds = c.TimeoutInSeconds,
                        UserAgent        = c.UserAgent
                    }
                };
                _config.Sections.Add(SectionName, configSection);
            }
            else
            {
                configSection.PageFetcherElement.SigninRetryTimes = c.SigninRetryTimes;
                configSection.PageFetcherElement.TimeoutInSeconds = c.TimeoutInSeconds;
                configSection.PageFetcherElement.UserAgent        = c.UserAgent;
            }

            _config.Save(ConfigurationSaveMode.Modified);
            System.Configuration.ConfigurationManager.RefreshSection(SectionName);
        }
Пример #2
0
        public PageFetcher(IPageFetcherConfig config, string authToken = null)
        {
            _config          = config;
            _cookieContainer = new CookieContainer();
            if (!string.IsNullOrEmpty(authToken))
            {
                var tokens = authToken.Split(',');
                foreach (var cookieInfo in tokens.Select(token => token.Split('=')))
                {
                    _cookieContainer.Add(new Cookie(cookieInfo[0], cookieInfo[1], "/", ".tgfcer.com"));
                }
                ;
                HasAuthToken = true;
            }
            var handler = new HttpClientHandler
            {
                CookieContainer       = _cookieContainer,
                UseCookies            = true,
                UseDefaultCredentials = false
            };

            _httpClient = new HttpClient(handler)
            {
                BaseAddress = new Uri("http://wap.tgfcer.com/"),
                Timeout     = TimeSpan.FromSeconds(_config.TimeoutInSeconds)
                              //_httpClient.GetAsync() throws TaskCanceledException
            };
            _httpClient.DefaultRequestHeaders.Add("user-agent", _config.UserAgent);
        }