using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace DragonVideoPlayer
{

    public static class UrlChecker
    {
        public static bool IsUrlAlive(string url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "HEAD";
                request.Timeout = 10000;
                request.AllowAutoRedirect = true;
               

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    return (int)response.StatusCode < 400;
                }
            }
            catch (WebException ex)
            {
                // HEAD拒否サイト用にGETで再試行
                if (ex.Response is HttpWebResponse err &&
                    (err.StatusCode == HttpStatusCode.MethodNotAllowed ||
                     err.StatusCode == HttpStatusCode.Forbidden))
                {
                    try
                    {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                        request.Method = "GET";
                        request.Timeout = 10000;
                        request.AllowAutoRedirect = true;
                        request.UserAgent = "Mozilla/5.0";

                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {
                            return (int)response.StatusCode < 400;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }

                return false;
            }
            catch
            {
                return false;
            }
        }
    }
}