我已经写了通过随机代理打开一个HttpURLConnection类的网站PROGRAMM。 我HttpURLConnection类被称为美国康涅狄格州 。 现在我知道,这其中的一些代理的可能太慢,所以我已经设置与连接至40000毫秒超时conn.setConnectTimeout(40000)
和conn.setReadTimeout(40000)
这样做之后,我得到这个代码:
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();
}
是有原因的连接可能会失败,所以在很多情况下,我获得了最后追赶块,并得到下面的输出:
消息:连接超时:连接MyTimeout:40000实际时间的流逝:21012
消息:连接超时:连接MyTimeout:40000实际时间的流逝:21016
消息:连接超时:连接MyTimeout:40000实际时间的流逝:21010
消息:连接超时:连接MyTimeout:40000实际时间的流逝:21009
所以我的问题是:我已经超时设置为40000毫秒,但我得到一个后约21000毫秒“连接超时” - 响应,有任何影响,你知道这是为什么?
编辑:使用Windows 7和我现在的IM添加e.printStackTrace()到catch块,如告诉记者,在意见。 感谢这么远。 输出现在是(例如):
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