Solved

Setting connection timeout with HttpURLConnection

Posted on 2003-11-17
12
669 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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