Example #1
0
        /// <summary>
        /// 检查友链
        /// </summary>
        public void CheckLinks()
        {
            var links = _linksService.LoadEntities(l => !l.Except).AsParallel();

            Parallel.ForEach(links, link =>
            {
                Uri uri           = new Uri(link.Url);
                HttpClient client = _httpClientFactory.CreateClient();
                client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("Mozilla/5.0"));
                client.DefaultRequestHeaders.Referrer = new Uri("https://masuit.com");
                client.Timeout = TimeSpan.FromHours(10);
                client.GetAsync(uri).ContinueWith(async t =>
                {
                    if (t.IsCanceled || t.IsFaulted)
                    {
                        link.Status = Status.Unavailable;
                        return;
                    }

                    var res = await t;
                    if (res.IsSuccessStatusCode)
                    {
                        link.Status = !(await res.Content.ReadAsStringAsync()).Contains(CommonHelper.SystemSettings["Domain"]) ? Status.Unavailable : Status.Available;
                    }
                    else
                    {
                        link.Status = Status.Unavailable;
                    }

                    _linksService.UpdateEntity(link);
                }).Wait();
            });
            _linksService.SaveChanges();
        }