Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
nmaddix
Posts: 111
Registered: ‎12-16-2008

How to force an HttpConnection to close in a timeout timer?

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()

waiting...

waiting...

0:10 - call HttpConnection.close() by timeout timer

waiting...

waiting...

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!

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008

Re: How to force an HttpConnection to close in a timeout timer?

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".

 

Please use plain text.
Developer
nmaddix
Posts: 111
Registered: ‎12-16-2008

Re: How to force an HttpConnection to close in a timeout timer?

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.

 

Please use plain text.