判断网址能不能访问
发布日期:2021-11-07 06:40:21 浏览次数:3 分类:技术文章

本文共 1393 字,大约阅读时间需要 4 分钟。

看了篇文章可能会用的上,直接贴过来了。

private bool UrlIsExist(String url){  System.Uri u = null;  try  {    u = new Uri(url);  }  catch { return false; }  bool isExist = false;  System.Net.HttpWebRequest r = System.Net.HttpWebRequest.Create(u) as System.Net.HttpWebRequest;  r.Method = "HEAD";  try  {    System.Net.HttpWebResponse s = r.GetResponse() as System.Net.HttpWebResponse;    if (s.StatusCode == System.Net.HttpStatusCode.OK)    {      isExist = true;    }  }  catch (System.Net.WebException x)  {    try    {      isExist = ((x.Response as System.Net.HttpWebResponse).StatusCode != System.Net.HttpStatusCode.NotFound);    }    catch { isExist = (x.Status == System.Net.WebExceptionStatus.Success); }  }  return isExist;}protected void Page_Load(object sender, EventArgs e){  Response.Write("
  • http://dotnet.aspx.cc/Images/ 是否存在:" + UrlIsExist("http://dotnet.aspx.cc/Images/")); Response.Write("
  • http://dotnet.aspx.cc/ImagesX/ 是否存在:" + UrlIsExist("http://dotnet.aspx.cc/ImagesX/")); Response.Write("
  • http://xxxxx/ 是否存在:" + UrlIsExist("http://xxxxx/")); Response.Write("
  • hxxxxxxxxxxxxxxxxxxx 是否存在:" + UrlIsExist("hxxxxxxxxxxxxxxxxxxx")); Response.Write("
  • http://dotnet.aspx.cc/Images/logoSite.gif 是否存在:" + UrlIsExist("http://dotnet.aspx.cc/Images/logoSite.gif")); Response.Write("
  • http://dotnet.aspx.cc/Images/logoSite2.gif 是否存在:" + UrlIsExist("http://dotnet.aspx.cc/Images/logoSite2.gif"));}
  • 原文地址: 

    转载地址:https://blog.csdn.net/yysyangyangyangshan/article/details/7411183 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

    上一篇:为什么你不被重视
    下一篇:c# 窗口句柄问题(转)

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2024年04月17日 05时21分46秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章