My app accesses info on the web, during my development how do I simulate a site is down in Java ? If the url gets me the page my app processes it, but if a site for some reason is temporarily down, or even if the site is up, but the url returns invalid info or it could be the Internet connection is disabled, how can a Java app distinguish those situations ? And be able to tell which is occurring ?
Edit : I'm trying to write my Java app so that when it encounters different url accesses, it knows which case it is dealing with and tell user accordingly.
The connect()
method from the java.net.URLConnection class throws a SocketTimeoutException
if it cannot connect to the given URL. You can set a timeout via the setConnectTimeout(int)
method and react on the SocketTimeoutException
exception which is thrown in this case.
For the purposes of testing or simulating different error conditions, here are a few additional suggestions:
As you test these different scenarios you will be surprised to learn what happens to your client. If you are lucky you will get exceptions. Sometimes the client may hang forever. But that's the real world. Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With