• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpClient: How do I abandon the request after 20 seconds?

 
Jason Bock
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for beginners. Moving.
 
Jason Bock
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah okay, got it.
Thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic