Take the 2-minute tour ×

I am writing some failover code so that if my desktop app cannot connect to its website, it can instead try a backup website.

However, I cannot seem to figure out how to simulate a test if a website is 'down' or not. If I try an obvoiusly incorrect url such as "http://www.mybadsite.badTLD" , my ISP provider sends me to a default catch page.

Whereas when a site is truly down and you cannot connect to it, you get the browser message saying it cannot connect. This is what I need to emulate.


share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Edit your hosts file to redefine the host you're trying to connect to. You can do (Or anything unreachable).

You can also do a test with - that returns a different error (Invalid IP). There may be some benefit to testing for that too.

Your ISP is redirecting for a DNS lookup failure, but anything resolved by your hosts file short-circuits that.

share|improve this answer
add comment

If you have a server that runs PHP, you could use the header() function to send headers for HTTP error codes back to your application.

header("HTTP/1.0 404: Not Found", true, 404);
header("HTTP/1.0 403: Forbidden", true, 403);
header("HTTP/1.0 500: Internal Server Error", true, 500);

These are some examples. Many server languages have methods for passing headers to the browser. Check this out for more error codes:


share|improve this answer
Although these messages are the result of a successful connection, it is worthwhile to test for these situations too. –  Lee Kowalkowski Mar 31 '11 at 20:18
They are various other states of 'down' outside of the server not existing. :D –  Justin Pearce Mar 31 '11 at 20:20
add comment

Assuming you do not have a server running locally...

share|improve this answer
I actually used your answer, since it was easiest. I apologize though, I accepted the other as the 'proper' answer, since "localhost" is usually just an entry in the HOSTS file. =) –  mikew Mar 31 '11 at 20:38
add comment

Your Answer


