posted 13 years ago
That's an interesting question. I found the JavaDocs to be a bit unclear on this point, but with a few experiments, I've found that
seem to succeed immediately without trying to contact the host. However:
will try to connect to the remote host.
URLConnection has two timeout methods, one for setting the read timeout, and another for setting the connect timeout. So if you set:
and it can't connect within 10 seconds, it will throw a SocketTimeoutException. Try it!
Another option is setting the system properties: sun.net.client.defaultConnectTimeout and sun.net.client.defaultReadTimeout. I think that would work, but you probably have to set them as you start the JVM, i.e., with a -D parameter.