Skip to main content
Notice removed Current answers are outdated by Murali
Bounty Ended with Amit Sharma's answer chosen by Murali

I've written a programm that opens a httpurlconnection to a website through random proxies. My httpurlconnection is called "conn"conn. Now I know, that some of those proxies might be too slow, so i've set the timeout of the connection to 40000 milliseconds with conn.setConnectTimeout(40000)conn.setConnectTimeout(40000) and conn.setReadTimeout(40000)conn.setReadTimeout(40000).

I've written a programm that opens a httpurlconnection to a website through random proxies. My httpurlconnection is called "conn". Now I know, that some of those proxies might be too slow, so i've set the timeout of the connection to 40000 milliseconds with conn.setConnectTimeout(40000) and conn.setReadTimeout(40000).

I've written a programm that opens a httpurlconnection to a website through random proxies. My httpurlconnection is called conn. Now I know, that some of those proxies might be too slow, so i've set the timeout of the connection to 40000 milliseconds with conn.setConnectTimeout(40000) and conn.setReadTimeout(40000).

Notice added Current answers are outdated by Murali
Bounty Started worth 50 reputation by Murali
added 2204 characters in body
Source Link
long diff = 0; long starttime = 0; long endtime = 0; try { starttime = System.currentTimeMillis(); conn.connect(); endtime = System.currentTimeMillis(); diff = endtime - starttime; if (endtime <= starttime + conn.getConnectTimeout()) { //Trying to read sourecode InputStreamReader isrConn = new InputStreamReader(conn.getInputStream()); BufferedReader brConn = new BufferedReader(isrConn); line = brConn.readLine(); while (line != null) { response += line + "\t"; try { line = brConn.readLine(); } catch (IOException e) { printError("Reading sourcecode failed."); } } } else { response = "blabla."; } // If conn.connect failed } catch (IOException e) { endtime = System.currentTimeMillis(); diff = endtime - starttime; response = "Message: "+e.getMessage() +" MyTimeout:"+ conn.getConnectTimeout() +" Actual time passed: "+ diff;   e.printStackTrace();  } 

So my question would be: I have set the timeout to 40000 milliseconds, but i get a "Connection timed out"-response after about 21000 milliseconds, does any of you know why that is?

EDIT: im using windows 7 and i now added the e.printStackTrace() to the catch-block, like told in the comments. thanks so far. the output now is (example):

java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient$1.run(Unknown Source) at sun.net.www.http.HttpClient$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.http.HttpClient.privilegedOpenServer(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.<init>(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) at TestThread.getSourcePage(TestThread.java:361) at TestThread.aChecker(TestThread.java:216) at TestThread.getNextProxy(TestThread.java:169) at TestThread.getNextC(TestThread.java:157) at TestThread.aChecker(TestThread.java:273) at TestThread.getNextProxy(TestThread.java:169) at TestThread.aChecker(TestThread.java:295) at TestThread.getNextProxy(TestThread.java:169) at TestThread.getNextC(TestThread.java:157) at TestThread.run(TestThread.java:103) at java.lang.Thread.run(Unknown Source) Message: Connection timed out: connect MyTimeout:40000 Actual time passed: 21015 
long diff = 0; long starttime = 0; long endtime = 0; try { starttime = System.currentTimeMillis(); conn.connect(); endtime = System.currentTimeMillis(); diff = endtime - starttime; if (endtime <= starttime + conn.getConnectTimeout()) { //Trying to read sourecode InputStreamReader isrConn = new InputStreamReader(conn.getInputStream()); BufferedReader brConn = new BufferedReader(isrConn); line = brConn.readLine(); while (line != null) { response += line + "\t"; try { line = brConn.readLine(); } catch (IOException e) { printError("Reading sourcecode failed."); } } } else { response = "blabla."; } // If conn.connect failed } catch (IOException e) { endtime = System.currentTimeMillis(); diff = endtime - starttime; response = "Message: "+e.getMessage() +" MyTimeout:"+ conn.getConnectTimeout() +" Actual time passed: "+ diff; } 

So my question would be: I have set the timeout to 40000 milliseconds, but i get a "Connection timed out"-response after about 21000 milliseconds, does any of you know why that is?

long diff = 0; long starttime = 0; long endtime = 0; try { starttime = System.currentTimeMillis(); conn.connect(); endtime = System.currentTimeMillis(); diff = endtime - starttime; if (endtime <= starttime + conn.getConnectTimeout()) { //Trying to read sourecode InputStreamReader isrConn = new InputStreamReader(conn.getInputStream()); BufferedReader brConn = new BufferedReader(isrConn); line = brConn.readLine(); while (line != null) { response += line + "\t"; try { line = brConn.readLine(); } catch (IOException e) { printError("Reading sourcecode failed."); } } } else { response = "blabla."; } // If conn.connect failed } catch (IOException e) { endtime = System.currentTimeMillis(); diff = endtime - starttime; response = "Message: "+e.getMessage() +" MyTimeout:"+ conn.getConnectTimeout() +" Actual time passed: "+ diff;   e.printStackTrace();  } 

So my question would be: I have set the timeout to 40000 milliseconds, but i get a "Connection timed out"-response after about 21000 milliseconds, does any of you know why that is?

EDIT: im using windows 7 and i now added the e.printStackTrace() to the catch-block, like told in the comments. thanks so far. the output now is (example):

java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient$1.run(Unknown Source) at sun.net.www.http.HttpClient$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.http.HttpClient.privilegedOpenServer(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.<init>(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) at TestThread.getSourcePage(TestThread.java:361) at TestThread.aChecker(TestThread.java:216) at TestThread.getNextProxy(TestThread.java:169) at TestThread.getNextC(TestThread.java:157) at TestThread.aChecker(TestThread.java:273) at TestThread.getNextProxy(TestThread.java:169) at TestThread.aChecker(TestThread.java:295) at TestThread.getNextProxy(TestThread.java:169) at TestThread.getNextC(TestThread.java:157) at TestThread.run(TestThread.java:103) at java.lang.Thread.run(Unknown Source) Message: Connection timed out: connect MyTimeout:40000 Actual time passed: 21015 
Source Link

Java HTTPUrlConnection timeout does not work

I've written a programm that opens a httpurlconnection to a website through random proxies. My httpurlconnection is called "conn". Now I know, that some of those proxies might be too slow, so i've set the timeout of the connection to 40000 milliseconds with conn.setConnectTimeout(40000) and conn.setReadTimeout(40000).

After doing so, i got this code:

long diff = 0; long starttime = 0; long endtime = 0; try { starttime = System.currentTimeMillis(); conn.connect(); endtime = System.currentTimeMillis(); diff = endtime - starttime; if (endtime <= starttime + conn.getConnectTimeout()) { //Trying to read sourecode InputStreamReader isrConn = new InputStreamReader(conn.getInputStream()); BufferedReader brConn = new BufferedReader(isrConn); line = brConn.readLine(); while (line != null) { response += line + "\t"; try { line = brConn.readLine(); } catch (IOException e) { printError("Reading sourcecode failed."); } } } else { response = "blabla."; } // If conn.connect failed } catch (IOException e) { endtime = System.currentTimeMillis(); diff = endtime - starttime; response = "Message: "+e.getMessage() +" MyTimeout:"+ conn.getConnectTimeout() +" Actual time passed: "+ diff; } 

There are reasons why the connection could fail, so in many cases i get to the last catch-block and get the following output:


Message: Connection timed out: connect MyTimeout:40000 Actual time passed: 21012

Message: Connection timed out: connect MyTimeout:40000 Actual time passed: 21016

Message: Connection timed out: connect MyTimeout:40000 Actual time passed: 21010

Message: Connection timed out: connect MyTimeout:40000 Actual time passed: 21009


So my question would be: I have set the timeout to 40000 milliseconds, but i get a "Connection timed out"-response after about 21000 milliseconds, does any of you know why that is?