C#判断网址是否可以访问

/// <summary>
        /// 判断网址是否可以访问
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        protected static bool CheckPageUrl(string url)
        {
            bool result = false;
            try
            {
                if (url.IndexOf("http").Equals(-1))
                {
                    url = "http://" + url;
                     Console.WriteLine(url);
                }
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
                myHttpWebRequest.Method = "GET";
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    result = true;
                }
                myHttpWebResponse.Close();
            }
            catch
            {
                result = false;
            }

            return result;
        }

posted @ 2021-08-13 11:00  ZhiPeng007  阅读(841)  评论(0编辑  收藏  举报