C# 判断网站是否能访问或者断链

时间:2022-03-25 16:00:08

参考网站:http://www.cnblogs.com/junny/archive/2012/10/30/2745978.html

public bool CheckUrlVisit(string url)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
          resp.Close(); 
return true;
}
}
catch (WebException webex)
{
return false;
} return false; }