Here is my code :
URL u = new URL("http://www.google.com");
URLConnection conn = u.openConnection();
conn.setConnectTimeout(3000);
conn.connect();
My network connection is sometimes unstable(I've connected to the wireless router but actually my router doesn't have Internet access). When that happens, this code will block for a lone time and finally throws UnknownHostException
. Why setConnectTimeout(3000)
doesn't work in this case? How to fix this?
Thanks!
------------update---------------
My guess is that conn.connect()
will query DNS first but there's no time limit for this operation. I've tried Socket
class and problem remains. setTimeout()
seems do not work for DNS query.