判断URL文件是不是在于在。

 

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

 

相关文章:

  • 2022-01-17
  • 2021-11-18
  • 2022-02-26
  • 2021-11-19
  • 2021-05-21
  • 2022-01-04
  • 2021-12-22
猜你喜欢
  • 2021-09-26
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
相关资源
相似解决方案