首页 > Windows开发 > 详细

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

时间:2021-08-13 15:01:29      阅读:25      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/zhipeng007/p/15136350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!