确定url是true还是false

时间:2013-01-16 15:02:26

标签: javascript asp.net

我使用ASP.NET和javascript,

我的代码中有一个问题

我需要确定是否存在网址

url="http://www.404.com"    
If url == exists then
{

}
else if url == not then
{

}

2 个答案:

答案 0 :(得分:2)

如果您想在C#中进行尝试,请尝试验证任何网址

private bool ValidateUrl(string url)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "HEAD";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        return (response.StatusCode == HttpStatusCode.OK);
    }
    catch
    {
        return false;
    }
}

答案 1 :(得分:-1)

您可以使用C#Ping和PingReply类。

private bool ValidateUrl(string url)
{
    try
    {
        Ping x = new Ping();
        int timeout=500;
        PingReply reply = x.Send(url,timeout); 
        if(reply.Status == IPStatus.Success)
          {
              return true;
          }
        else
          {
              return false;
          }
    }
    catch
    {
        return false;
    }
}