根据我的理解,connectionTimeout
是连接后第一个请求发送的时间。如果第一个请求的时间超过connectionTimeout
,就会抛出一个错误。
我说的对吗?因此,我编写了以下代码片段,但它不起作用。
也许我误解了一些概念。非常感谢。
HttpURLConnection conn = (HttpURLConnection) new URL("http://127.0.0.1:8080/MVN.EXAMPLE/hello/HelloServlet").openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
try {
conn.connect();
Thread.sleep(65*1000L); // the default connectionTimedout is 20s.
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
catch (Exception e) {
int aaa = 0; // for debug
// If connectionTimeout is triggered, it will go here.
}
finally {
int bbb = 0; // for debug
}
URL
“http://127.0.0.1:8080/MVN.EXAMPLE/hello/HelloServle”是我在 Tomcat
中的本地应用程序的路径。
最佳答案
我在tomcat端使用telnet模拟connectionTimeout。
- 使用远程登录 127.0.0.1 8080
- Wait connectionTimeout//默认20s
- Telnet 将在 20 秒后被外国主机关闭
- 重复第 1 步
- 键入 GET/XXXX,telnet 将得到响应
因此,这可以表明telnet只连接到主机,而没有发送请求。因此,当达到connectionTimeout时,telnet客户端将关闭,这是由tomecat触发的——它会自动关闭客户端。
关于java - 如何在Tomcat中模拟connectionTimeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539971/