Meaningless Drivel is fun!*
The moose likes Web Services and the fly likes HttpClient: How do I abandon the request after 20 seconds? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "HttpClient: How do I abandon the request after 20 seconds?" Watch "HttpClient: How do I abandon the request after 20 seconds?" New topic
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
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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: 39478
    
  28
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: 18708
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpClient: How do I abandon the request after 20 seconds?