12

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.

Thanks

mikew
  • 1,634
  • 2
  • 15
  • 25

6 Answers6

10

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

You can also do a test with 0.0.0.0 - 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.

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
9

In order to test how your scripts deal with different responses I'd rather use a special server that generates different HTTP codes. One of them is httpstat.us

In your case, the correct http status code is 503 (Service Unavailable).

The 503 status code indicates that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance

which will likely be alleviated after some delay.

For more information on http status codes I suggest reading This IETS Documentation

Community
  • 1
  • 1
PinguinoNero
  • 123
  • 1
  • 6
5
http://localhost

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

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
  • 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
5

Stumbling upon this on 2020, I found this:

https://www.mocky.io/

https://httpstat.us/

jonayreyes
  • 538
  • 1
  • 10
  • 27
1

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);
or
header("HTTP/1.0 403: Forbidden", true, 403);
or
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:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Justin Pearce
  • 4,994
  • 2
  • 24
  • 37
0

I used this web service as follows:

curl -v http://httpstat.us/<http status code>

For example:

curl -v http://httpstat.us/503

The result is:

> GET /503 HTTP/1.1
> Host: httpstat.us
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 503 Service Unavailable
...