gpt4 book ai didi

Java HTTPUrlConnection 超时不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:51:32 24 4
gpt4 key购买 nike

我编写了一个程序,它通过随机代理打开到网站的 httpurl 连接。我的 httpurl 连接称为 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-block 并获得以下输出:


消息:连接超时:connect MyTimeout:40000 实际耗时:21012

消息:连接超时:connect MyTimeout:40000 实际耗时:21016

消息:连接超时:connect MyTimeout:40000 实际耗时:21010

消息:连接超时:connect MyTimeout:40000 实际耗时:21009


所以我的问题是:我已将超时设置为 40000 毫秒,但我在大约 21000 毫秒后收到“连接超时”响应,有人知道这是为什么吗?

编辑:我使用的是 Windows 7,我现在将 e.printStackTrace() 添加到 catch-block,如评论中所述。谢谢到目前为止。现在的输出是(示例):

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.SocketTimeoutException 而不是 java.net.ConnectException

那么,是什么导致了 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 是所有配置都正确的快乐路径
  • 在案例 #4 中,我们得到一个 java.net.SocketTimeoutException,因为 java 进程能够建立连接(到代理端口),但没有获取任何数据作为目标主机的端口读取号码无效
  • 在案例 #2、#5 中,我们得到 java.net.ConnectException,因为 java 进程尝试写入/读取的端口无效
  • 连接超时值不适用于没有进程监听 java 进程尝试连接的端口的情况。这就是为什么您在超时到期之前获得 ConnectException 的原因


消息:连接被拒绝:连接 MyTimeout:10000 实际耗时:6549
java.net.ConnectException:连接被拒绝:连接
在 java.net.DualStackPlainSocketImpl.waitForConnect( native 方法)
在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
....
....

结论:

  • 您尝试连接的某些代理肯定已关闭。因此 java 进程抛出 java.net.ConnectException
  • 最好捕获java.net.ConnectException并将代理标记为无效/关闭

关于Java HTTPUrlConnection 超时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com