| Author |
HttpClient: How do I abandon the request after 20 seconds?
|
Jason Bock
Greenhorn
Joined: Dec 27, 2008
Posts: 19
|
|
I'm using HttpClient 4.0, and I'm trying to set a timeout for my connection.
All the examples I've found online refer to HttpClient 3, so none of them are working for me.
I would like to drop/cancel the connection request after 20 seconds have passed, and continue with the rest of the program.
I've set the timeout to 20 seconds, but it throws an exception instead of executing the rest of the code..
client.getParams().setParameter("http.socket.timeout", 20000);
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Well the exception behavior looks fine to me. I've not used that library, but if the method call throws an exception after the timout period, then you just need to catch it and do whatever is necessary...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
Too difficult a question for beginners. Moving.
|
 |
Jason Bock
Greenhorn
Joined: Dec 27, 2008
Posts: 19
|
|
What would be the correct way to measure if 20 seconds have passed so I could manually close the connection?
Is there a timer/clock in Java I could utilize for this?
It's just that the page is always going to time out, because the request I'm sending is to shutdown a server, so I'd like to avoid stuffing the rest of the program into the exception block.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
No, Ankit is right. Don't muck about with setting timers or any of that. Just like this:
try {
send request
} catch (Exception)
do nothing
}
continue with the rest of your tasks
|
 |
Jason Bock
Greenhorn
Joined: Dec 27, 2008
Posts: 19
|
|
Ah okay, got it.
Thanks guys.
|
 |
 |
|
|
subject: HttpClient: How do I abandon the request after 20 seconds?
|
|
|