Java 中 URL 的默认超时是多少?这对 url.openStream() 有何影响?

What is the default timeout for URL in Java? How does this affect url.openStream()?

Java 中 URL 的默认超时是多少? 这对 url.openStream() 有何影响?

有两种类型的超时:connection timeoutread timeout。 对于连接超时和读取超时,默认值为 -1 (infinity)

openStream() 建立连接并提供输入流,因此 openStream() 会受到连接超时和读取超时的影响。

JDK 中的以下代码解释了这一点:

package sun.net

public class NetworkClient {
    /* Default value of read timeout, if not specified (infinity) */
    public static final int DEFAULT_READ_TIMEOUT = -1;

    /* Default value of connect timeout, if not specified (infinity) */
    public static final int DEFAULT_CONNECT_TIMEOUT = -1;
    ...
}