java.net.URL替代自定义超时设置 [英] Alternative to java.net.URL for custom timeout setting

查看:471
本文介绍了java.net.URL替代自定义超时设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要使用java.net.URL类进行远程数据请求的超时设置。在一些谷歌搜索后发现有两个系统属性可用于设置URL类的超时,如下所示。

Need timeout setting for remote data request made using java.net.URL class. After some googling found out that there are two system properties which can be used to set timeout for URL class as follows.

sun.net.client.defaultConnectTimeout  
sun.net.client.defaultReadTimeout

我没有控制所有系统,不希望每个人都继续设置系统属性。是否有任何其他替代方法可以进行远程请求,这将允许我设置超时。
没有任何库,如果在java本身可用则更可取。

I don't have control over all the systems and don't want everybody to keep setting the system properties. Is there any other alternative for making remote request which will allow me to set timeouts. Without any library, If available in java itself is preferable.

推荐答案

如果你打开一个 URLConnection 来自 URL 您可以通过这种方式设置超时:

If you're opening a URLConnection from URL you can set the timeouts this way:

URL url = new URL(urlPath);
URLConnection con = url.openConnection();
con.setConnectTimeout(connectTimeout);
con.setReadTimeout(readTimeout);
InputStream in = con.getInputStream();

你如何使用 URL 或者什么你把它传给了吗?

How are you using the URL or what are you passing it to?

这篇关于java.net.URL替代自定义超时设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆