In my Java client app I'm connecting to my server using HttpsURLConnection. This works all fine, except on certain moments it does not timeout at all. I set the timeout parameters of the connection as such (to 5000 ms):
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
// timeouts
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.setRequestMethod("put");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-length", String.valueOf(output.length()));
OutputStreamWriter os = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
BufferedWriter writer = new BufferedWriter(os);
writer.write(output);
writer.close();
os.close();
con.connect();
int respCode = con.getResponseCode();
if(respCode == 200) {
InputStreamReader is = new InputStreamReader(con.getInputStream());
BufferedReader br = new BufferedReader(is);
String input;
String respBody = "";
while ((input = br.readLine()) != null){
respBody += input;
}
br.close();
is.close();
this.result = respBody;
}
I'm catching all exceptions, but none is registered, so the problem cannot be an uncaught exception. The connection simply does not timeout. I waited for 3 minutes and had no response. I could reproduce the bug a few times while switching network during the execution of a request.
Also, sometimes there was a response after about a minute. Which is longer than the timeouts.
I already tried interrupting the thread on which the request is made after a timeout, disconnecting the HttpsURLConnection. But there must be a neater solution than that. I would like to know where this indefinite timeout comes from.
Any ideas?
Thanks a lot!