Java HTTPUrlConnection超时不起作用
我写了一个程序,通过随机代理打开一个网站的httpurl连接.我的httpurlconnection被称为conn.现在我知道,有些代理可能太慢,所以我已经使用conn.setConnectTimeout(40000)和conn.setReadTimeout(40000)将连接的超时设置为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(); } 有连接可能会失败的原因,所以在很多情况下,我得到最后一个catch块并得到以下输出: 消息:连接超时:连接MyTimeout:40000实际通过时间:21012 消息:连接超时:连接MyTimeout:40000实际传递时间:21016 消息:连接超时:连接MyTimeout:40000通过的实际时间:21010 消息:连接超时:连接MyTimeout:40000通过的实际时间:21009 所以我的问题是:我将超时设置为40000毫秒,但是在大约21000毫秒之后我得到一个“连接超时” – 响应,你们有没有知道为什么呢? 编辑:im使用windows 7和我现在添加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 解决方法
看看你得到的例外:
最大的线索:你得到java.net.ConnectException 根据javadoc,java.net.ConnectException表示连接被远程拒绝,原因是没有进程在端口上侦听. public class ConnectException extends SocketException Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically,the connection was refused remotely (e.g.,no process is listening on the remote address/port) 您在HttpUrlConnection中配置的内容 那么是什么原因导致java.net.ConnectException? +------------+------------+----------------+------------------+---------------------------------+ | Valid Host | Valid Port | Valid Proxy IP | Valid Proxy Port | Exception | +------------+------------+----------------+------------------+---------------------------------+ #1 | yes | yes | -NA- | -NA- | -- none -- | #2 | yes | no | -NA- | -NA- | java.net.ConnectException | +------------+------------+----------------+------------------+---------------------------------+ #3 | yes | yes | yes | yes | -- none -- | #4 | yes | no | yes | yes | java.net.SocketTimeoutException | #5 | yes | yes | yes | no | java.net.ConnectException | +------------+------------+----------------+------------------+---------------------------------+ >案例#1,#3是所有配置正确的快乐路径 消息:连接拒绝:连接MyTimeout:10000实际传递时间:6549 结论: >您尝试连接的某些代理必须关闭.因此java进程抛出java.net.ConnectException>最好抓住java.net.ConnectException并将代理标记为invalid / down (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |