Link to home
Start Free TrialLog in
Avatar of vinay_delhi
vinay_delhi

asked on

Setting connection timeout with HttpURLConnection

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.
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>
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.
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.
>>Then post how.

Already been posted
They are not available in 1.3.
I can assure you they are - I use them all the time
u must be running 1.4 then
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
> but there's also one for 1.3 as well

I think you are wrong again :)