编程语言
首页 > 编程语言> > C#判断网址的可访问性

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;
        }

作者:艾孜尔江

标签:myHttpWebResponse,false,访问,C#,HttpWebResponse,网址,result,url,myHttpWebRequest
来源: https://www.cnblogs.com/ezhar/p/12864953.html