12-28-2011 04:49 PM
I have an app that makes an http connection to my server and I'm trying to make it more robust. Since I can't control the timeout on all connection types, I've set up my own timer to come back and close the connection after a timeout time if it's not already closed. However, calling HttpConnection.close() seems to block and not close the connection for a while sometimes.
By adding a sleep(30) call to my server code so that it keeps the connection open for 30 seconds, I can see that the close() call blocks for 30 seconds, meaning it doesn't really close the connection until the server responds. This is definitely not what I want.... a timeout should immediately close the connection and not block.
Here's an example timeline using a 10 second timeout...
0:00 - call HttpConnection.open()
0:00 - call HttpConnection.getResponseCode()
0:10 - call HttpConnection.close() by timeout timer
0:30 - getResponseCode() returns 200
0:30 - getResponseMessage() throws IOException "Stream closed"
Any advice on how to force the connection to close when my timeout timer fires?
Thanks and happy new year!
12-28-2011 05:45 PM
In my experience, you can't force it to close.
In our HTTP engine, we call a "cancel request" method which simply sets a value in the engine that says "we're not waiting anymore, close and discard whatever you got back".
12-28-2011 05:49 PM
Ahhh ok thanks. Good to hear of a solid real-world solution. I was hoping it wouldn't come to that since my thread is blocking on the getResponseCode() call. I'll have to re-write.