Library code snippets

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.");
}

Comments

  1. 05 Feb 2009 at 13:55
    [A link should have this http like these http://www.dcglobal.us/contactus.htm](http://www.dcglobal.us/contactus.htm)
  2. 03 Feb 2009 at 01:03
    [code] public static bool doesUrlExist(string manifestUrl) { bool urlExists = false; Uri sourceUri = new Uri(manifestUrl); // Use Windows Authentication to check for a matching message directory HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sourceUri); request.Credentials = CredentialCache.DefaultCredentials; request.Method = "HEAD"; try { // Retrieve the response from the server for the manifest.xml file HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.ContentLength > 0) // if the file is there { urlExists = true; } // Force the connection closed response.Close(); } catch (WebException) { return false; } return urlExists; } [/code] Can also say "if (response.ContentLength > 0)" that it is equal to true, can hard-code credentials, like this: [code] bool urlExists = false; string userName = "myUser"; string password = "Password"; string domain = "MYDOMAIN"; // Use Windows Authentication to check for a matching message directory WebRequest request = WebRequest.Create(manifestUrl); CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri(manifestUrl), "NTLM", new NetworkCredential(userName, password, domain)); request.Credentials = myCache; // CredentialCache.DefaultCredentials; request.Method = "HEAD"; [/code] and instead of a response.Close(); at the end, you could encapsulate it all in brackets with a "using" statement. If your network sucks and you don't always get a valid reply, even though you should, you can use the function in a loop: [code] bool tester = false; // Use a function that will determine if the URL exists while ((tester == false) && (urlCounter < 51)) { tester = doesUrlExist(manifestUrl); urlCounter++; } urlCounter = 0; [/code] -Tom
  3. 06 Mar 2007 at 10:45

    use System.net,System.web namespace

    HttpWebRequest req =(HttpWebRequest) HttpWebRequest.Create(@txtURL.Text.ToString());

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();

    Response.Write(res.StatusCode.ToString());

  4. 25 May 2004 at 02:45

    this check only host name.
    And url like http://www.developerfusion.com/forums/default.aspx?


    Thanks

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of How to check to make sure a URL is valid.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay 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.

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Want to stay in touch with what's going on? Follow us on twitter!