冠昇 黃
冠昇 黃

Reputation: 39

determine url is true or false

I use ASP.NET with javascript ,

I have a question in my code

I need to determine url exists or not

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

}
else if url == not then
{

}

Upvotes: 1

Views: 221

Answers (2)

Murali Gangineni
Murali Gangineni

Reputation: 83

You can use C# Ping and PingReply classes.

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

Upvotes: -1

James
James

Reputation: 936

If you want to do it in C# try this out, it will validate any url for you

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

Upvotes: 2

Related Questions