shows the status of the website in a ListBox as
a ListBox item -
private void buttonCheckNow_Click(object sender, RoutedEventArgs e)
{
string website = null;
website = "http://www.csharpersworld.com";
if (ConnectionAvailable(website))
{
listBoxStatus.Items.Add("Status OK " + website + " " + DateTime.Now.ToShortTimeString());
}else{
listBoxStatus.Items.Add("Status FAILED " + website + " " + DateTime.Now.ToShortTimeString());
}}
the method -
public bool ConnectionAvailable(string WebSiteString)
{
try
{
HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(WebSiteString);
HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
if (HttpStatusCode.OK == rspFP.StatusCode)
{
// HTTP = 200 connection available, server online
rspFP.Close();
return true;
}else
{
// Other status - Server,connection not ok
rspFP.Close();
return false;
}}
catch (WebException)
{
// Exception - connection not available
return false;
}}