Solved

Setting connection timeout with HttpURLConnection

Posted on 2003-11-17
12
739 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 25 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

729 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