Reputation: 1048
i have tried to timeout httpurlconnection in the following manner
URL urlConnect = new URL(url.toString());
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection urlc = (HttpURLConnection) urlConnect.openConnection();
urlc.setConnectTimeout(1000*5);
urlc.setReadTimeout(1000*5);
urlc.connect();
but failed to connect, its taking the timeout assigned by apache tomcat of 2 minutes to timeout instead of 5 seconds gthat i have provided, in such cases how can in manually timeout the httpurlconnection
Upvotes: 1
Views: 7772
Reputation: 41123
Following is what Java manual say for both connectTimeout and readTimeout:
connectTimeout
Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
readTimeout
Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
So I think in your case, you manage to establish communications link in less than 5 seconds, and you did not read any input stream. Hence the behavior is correct (wait until Apache times out)
Upvotes: 6