Solved

Setting connection timeout with HttpURLConnection

Posted on 2003-11-17
12
651 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
  • 5
  • 4
12 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 25 total points
Comment Utility
 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
Comment Utility
>>
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
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Then post how.

Already been posted
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
Comment Utility
They are not available in 1.3.
0
 
LVL 86

Expert Comment

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

Expert Comment

by:objects
Comment Utility
u must be running 1.4 then
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
> but there's also one for 1.3 as well

I think you are wrong again :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now