[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Setting connection timeout with HttpURLConnection

Posted on 2003-11-17
12
Medium Priority
?
782 Views
Last Modified: 2007-12-19
I am using :

URL url = new URL(<some url>);
HttpURLConnection huc= (HttpURLConnection) url.openConnection();

I want the connection to close if its not able to establish connction (or anything goes wrong during getting connection) afer a given amount of time. I don't want it to hang up the connection for ever.

How can I do that ?

Thanks.
0
Comment
Question by:vinay_delhi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
12 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 9769582
 What JDK are you using? If you are using 1.4 there are a few new properties that you can use to set the timeout on a URLConnection.

  The properties you need are sun.net.client.defaultConnectTimeout and sun.net.client.defaultReadTimeout and you can set them by doing:

Properties p = System.getProperties();
// Delay for 10 seconds.. The daleay should be in milliseconds
p.put("sun.net.client.defaultConnectTimeout","10000");
p.put("sun.net.client.defaultReadTimeout","10000");
System.setProperties(p);

  For more info read: http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html

  If you are not using jdk1.4 you will have to manually implement some sort of timeout. YOu can get the InputStream and check for available() bytes from the connection and time it out after some time. Unless you use the nio package the input stream should block until it receives some data. For sample code take a look here: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20368108.html?query=timeout&searchType=topic

  The other option is to start a separate thread and interrupt it after on time out.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9770107
>>
If you are using 1.4 there are a few new properties that you can use to set the timeout on a URLConnection.
The properties you need are sun.net.client.defaultConnectTimeout and sun.net.client.defaultReadTimeout and you can set them by doing:
>>

It's not necessary to have 1.4. to do this. At least, it can be done quite effectively in 1.3.
0
 
LVL 92

Expert Comment

by:objects
ID: 9774878
CEHJ> At least, it can be done quite effectively in 1.3.

Then post how.


My suggestioon would be to use a decent http implementation that provides support for timeout such as HTTPClient.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 9774909
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9775095
>>Then post how.

Already been posted
0
 
LVL 92

Expert Comment

by:objects
ID: 9775141
They are not available in 1.3.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9775279
I can assure you they are - I use them all the time
0
 
LVL 92

Expert Comment

by:objects
ID: 9775295
u must be running 1.4 then
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9775610
No - i tell a lie. Those *are* the 1.4 timeout properties, but there's also one for 1.3 as well - but i'm damned if i can find it at the moment
0
 
LVL 92

Expert Comment

by:objects
ID: 9775633
> but there's also one for 1.3 as well

I think you are wrong again :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question