开发者

Can I set a timeout for the data transfer in a Java HttpURLConnection?

I use

URL开发者_运维知识库Connection urlConn = theURL.openConnection();

// set connect timeout.
urlConn.setConnectTimeout(5000);

// set read timeout.
urlConn.setReadTimeout(5000);

in my application. One timeout for the connection time, on for the time until the read starts. Is there any simple way to timeout the actual data transfer process?

I have threads reading data from either very slow hosts, or the data source is very big, what leads to too long transfer times. How can I limit that time?


You could try using the Thread.join(int) -> http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#join(long). Then check the status of the thread once that's done. If it has not ended then terminate it and assume a timeout.


As far as I understood, and I think the docs support this, readTimeout starts from when the reading of the InputStream starts - so this will time out for a long transfer.

Edit: the docs say:

A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource

this is possibly not that well written, but sounds to me as if this does indeed pertain to transfer time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜