Library code snippets
How to check to make sure a URL is valid
By Edward Tanguay, published on 06 Jan 2004
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.");
}
Related articles
Related discussion
-
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records
by virtualking (0 replies)
-
How to optimize mysql subquery performance?
by Jayaram P (0 replies)
-
C# video Editing/rendering
by pkuchaliya (0 replies)
-
How to Fill DataSet with more records (around 1 lakh) in a faster way
by Jayaram P (0 replies)
-
Can't print on the network with MSADESS ??
by anatha1 (2 replies)
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...
use System.net,System.web namespace
HttpWebRequest req =(HttpWebRequest) HttpWebRequest.Create(@txtURL.Text.ToString());
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Response.Write(res.StatusCode.ToString());
this check only host name.
And url like http://www.developerfusion.com/forums/default.aspx?
Thanks
This thread is for discussions of How to check to make sure a URL is valid.