Categories
Asp, Asp.net

How To Check Website URL Exists Or Not Using C#

How To Check Website URL Exists Or Not Using C#

In this tutorial we will explain How To Check Website URL Exists Or Not Using C#.Sometime during registration we require website URL or web address, to check that URL or web address is correct this tip is very useful.

Code:

using System.Net;

protected void btnwebStatus_Click(object sender, EventArgs e)
{
bool status = CheckUrlStatus(txtWeb.Text);
if (status == true)
{
lbstatus.Text = "Yes the website exists!!!";
}
else
{
lblstatus.Text = "Oops the website DOES NOT exists!!!";
}
}
protected bool CheckUrlStatus(string Website)
{
try
{
var request = WebRequest.Create(Website) as HttpWebRequest;
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
return false;
}
}