简体   繁体   中英

Alternative to java.net.URL for custom timeout setting

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

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.

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();

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

一个常见的替代品是Apache Commons HttpClient ,它可以更好地控制获取HTTP URL的整个过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM