How to control the timeout of the whole session with HttpUrlConnection?

B

billdavidcn

How to control the timeout of the whole session with
HttpUrlConnection?

I find the following fact during test:
HttpUrlConnection.setConnectTimeout can only control the timeout of
connect operation, while setReadTimeout can only control the timeout
of read.
I have done an experiment and set timeout value in the following way:

m_connection.setConnectTimeout(1000);
m_connection.setReadTimeout(20 * 1000);

And in server side, I add the following sentence to make it delay to
response:
Thread.sleep(60 * 1000);

The result shows, client doesn't close the session in 20s, and the
communication completes after 60s when server responses it at last.

How could I close the session in 20s? I think to add a Timer should be
a solution, but is there any better choice?
 
S

SadRed

How to control the timeout of the whole session with
HttpUrlConnection?

I find the following fact during test:
HttpUrlConnection.setConnectTimeout can only control the timeout of
connect operation, while setReadTimeout can only control the timeout
of read.
I have done an experiment and set timeout value in the following way:

m_connection.setConnectTimeout(1000);
m_connection.setReadTimeout(20 * 1000);

And in server side, I add the following sentence to make it delay to
response:
Thread.sleep(60 * 1000);

The result shows, client doesn't close the session in 20s, and the
communication completes after 60s when server responses it at last.

How could I close the session in 20s? I think to add a Timer should be
a solution, but is there any better choice?
client doesn't close the session in 20s
Read would timeout and an exception would be thrown. What your client
were doing in that 20s?
 
B

billdavidcn

Thanks.
I have made a mistake, I have not handled the right exception, the
exception should be SocketTimeoutException.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top