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
Solved

Setting connection timeout with HttpURLConnection

Posted on 2003-11-17
12
681 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 49
servlet example 17 51
hibernate jars 4 45
running on tomcat not jboss eap 7.0 3 18
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

839 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