2

I have been toying with getting this working for a while now to no avail.

I want to check if a website is available and display its status in a label. I have no code to share as I haven't got this anywhere close. I am using VS 2013 building a asp.net website using VB.

I thought I could just ping the website but after multiple test using command.exe the website doesn't respond to pings even when up.

I know the page will be taken offline periodically for updates as this happened last week and when it is you get page cannot be displayed. I need to test if the page is online and return true if it is and false if not.

Silentbob
  • 2,805
  • 7
  • 38
  • 70
  • Is it your website or a 3rd party's? If you have control over the site you could dump a small text file in the root with the status, means if it's down for maintenance etc. you could update this and read it from your app. – James Nov 01 '13 at 13:43
  • ping is an ICMP tool that *might* have told you if the server was switched on but would have given you no information about what services (except Ping) were running. Is it enough to check that a page is returned when trying to access the website? What if that page just contains the text "this service is not available"? – Damien_The_Unbeliever Nov 01 '13 at 13:44
  • A lot of websites won't respond to ping so that's definitely not a reliable method. – nycdan Nov 01 '13 at 13:52

2 Answers2

2

The simple way is to do an httpWebRequest and examine the result. If the page doesn't exist, you will get an error that indicates that. I'm not as familiar with .webClient but that apparently works as well.

This past question gives you examples of both.

Community
  • 1
  • 1
nycdan
  • 2,819
  • 2
  • 21
  • 33
1

You can easily do this using HttpWebRequest.

try {
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("siteAddress");
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();
}
catch(WebException e) {
    //There is a problem accessing the site
}
Tim Ebenezer
  • 2,714
  • 17
  • 23