22

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.

2 Answers 2

63

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?

Sign up to request clarification or add additional context in comments.

2 Comments

He's likely plain calling URL#openStream() which will indeed use the default timeouts.
read timeout works perfectly, but connect timeout seams does not work
2

A common replacement is the Apache Commons HttpClient, it gives much more control over the entire process of fetching HTTP URLs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.