Discussion:
HttpURLConnection timeout?
(too old to reply)
Jack
2004-04-14 23:59:05 UTC
Permalink
Hi,

Is it possible to set a timeout when opening a connection to an
HttpURLConnection?

For example, is it possible to make the connection call and limit it to 5
seconds before a response is generated?

That is, I don't want to have to wait for the "default" timeout.

Thanks for any info.

Jack
Dave Ockwell-Jenner
2004-04-16 19:05:58 UTC
Permalink
Post by Jack
Hi,
Is it possible to set a timeout when opening a connection to an
HttpURLConnection?
For example, is it possible to make the connection call and limit it to 5
seconds before a response is generated?
That is, I don't want to have to wait for the "default" timeout.
[snip]

You can't do this using Sun's classes unless you use a "private"
property setting. Unfortunately, the setting is not guaranteed to
remain the same (or remain at all).

I've just been working on a similar task and used the
Commons-HTTPClient[1] from Apache. It allows you to specify both a
connection timeout and also a timeout when reading the response. It
works superbly!

[1] http://jakarta.apache.org/commons/httpclient/
--
Dave Ockwell-Jenner
Solar Nexus Solutions
http://www.solar-nexus.com/
Adam P. Jenkins
2005-04-13 01:59:30 UTC
Permalink
Post by Dave Ockwell-Jenner
Post by Jack
Hi,
Is it possible to set a timeout when opening a connection to an
HttpURLConnection?
For example, is it possible to make the connection call and limit it to 5
seconds before a response is generated?
That is, I don't want to have to wait for the "default" timeout.
[snip]
You can't do this using Sun's classes unless you use a "private"
property setting. Unfortunately, the setting is not guaranteed to
remain the same (or remain at all).
I've just been working on a similar task and used the
Commons-HTTPClient[1] from Apache. It allows you to specify both a
connection timeout and also a timeout when reading the response. It
works superbly!
[1] http://jakarta.apache.org/commons/httpclient/
Actually Sun has added this in Java 1.5. See the java.net.URLConnection
setConnectionTimeout() and setReadTimeout() methods. If you need
to use an earlier version of Java then the Jakarta HTTPClient package
works great.

Loading...