<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>How to check to make sure a URL is valid</Title>
      <Shortcut>HowtochecktomakesureaURLisvalid</Shortcut>
      <Description>How to check to make sure a URL is valid [C#]</Description>
      <Author>Robert Wagner</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=ee19fad1-a90a-45f0-a4de-15f423c318c5</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[//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:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Sockets" %>
<script runat="server">
    public void Page_Load(Object sender, EventArgs E) {
		string url = "www.google.com";
		if(UrlIsValid(url)) {
			Response.Write("The URL '" + url + "' is valid.");
		} else {
			Response.Write("The URL '" + url + "' is NOT valid.");
		}
    }
	public static bool UrlIsValid(string smtpHost)
	{
		bool br = false;
		try {
			IPHostEntry ipHost = Dns.Resolve(smtpHost);
			br = true;
		}
		catch (SocketException se) {
			br = false;
		}
		return br;
	}
</script>]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>