We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 23,757 times

Related Categories

How to check to make sure a URL is valid

If you have users enter URLs and you would like to check them to make sure they exist before you save them to the database, here is the code:

public static bool UrlIsValid(string smtpHost)
{
    bool br = false;
    try {
        IPHostEntry ipHost = Dns.Resolve(smtpHost);
        br = true;
    }
    catch (SocketException se) {
        br = false;
    }
    return br;
}

To use, simply call UrlIsValid with the host name, for example:

string url = "www.google.com";
if(UrlIsValid(url)) {
    Response.Write("The URL '" + url + "' is valid.");
} else {
    Response.Write("The URL '" + url + "' is NOT valid.");
}

Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Comments