gpt4 book ai didi

java - java.net.HttpURLConnection 默认超时记录在哪里?

转载 作者:行者123 更新时间:2023-12-02 01:52:02 24 4
gpt4 key购买 nike

java.net.HttpURLConnection类允许您设置连接超时和读取超时。但是,如果您不指定读取和连接超时的值,Javadoc 页面不会指定这些值。

默认连接和读取超时是否记录在任何地方?我能够通过编写一个简单的程序凭经验确定它们:

public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com");
URLConnection conn = url.openConnection();
System.out.println(String.format("connection timeout: %d", conn.getConnectTimeout()));
System.out.println(String.format("read timeout: %d", conn.getReadTimeout()));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}

(在 MacOS 和 Linux 上,对于我运行的 Java 1.8 版本,它们都是 0,表示没有超时)。

但是,我真正想要的是一个包含相关官方文档的 URL。

最佳答案

HttpUrlConnection 继承了 UrlConnection 的超时字段和 getter/setter 方法

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/net/URLConnection.java

/**

* @since 1.5

*/

private int connectTimeout;
private int readTimeout;

由于它们在声明时从未被分配其他值,因此它们为 0。文档明确提到非标准实现可以选择完全忽略超时变量并定义自己的实现或字段或静态值。

Sun's implementation of HttpURLConnection does this.

/* Redefine timeouts from java.net.URLConnection as we need -1 to mean
* not set. This is to ensure backward compatibility.
*/
private int connectTimeout = NetworkClient.DEFAULT_CONNECT_TIMEOUT;
private int readTimeout = NetworkClient.DEFAULT_READ_TIMEOUT;

关于java - java.net.HttpURLConnection 默认超时记录在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823893/

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