Saturday, March 29, 2008

URLConnection is silently retrying POST request

Recently, I was debugging an issue with a webservice that takes long time to respond. I noticed that the client was using the URLconnection object and it was automatically retrying the connection if the server abruptly terminated the connection. I was not expecting the client to reconnect to the server as I did not find any client side code to retry the connection. After googling for similar symptoms I found a known bug (ID 6382788) with the URLConnection object. Below is the explanation of the bug and the work around suggested by sun.
Non-idempotent http POST requests are being automatically resent to the server if the
server does not respond with a valid http response or an IOException occurs. This is
incorrect according to the rfc. Only idempotent requests are supposed to be
automatically resent.

As this has been the behavior of Sun's http client forever, some users may be relying
on this behavior without even knowing it. So the solution is to allow this behavior
to be configured through a system property, determines if the an unsuccessful http POST request will be
automatically resent to the server. Unsuccessful in this case means the server did
not send a valid http response or an IOException occurred. The default value is true
to maintain backward compatibility.
By the way the connection was reset because of a firewall that was terminating the connection as it was taking longer than the defined idle timeout.

No comments: