I'm using HttpURLConnection
on Android KitKat to POST some data to a server. The server takes a long time to respond, and the connection is silently retrying 1 to 3 times before timing out. I don't want it to retry, since the server acts on all of the requests, resulting in Bad Things(TM).
I've tried System.setProperty("http.keepAlive", "false")
before opening the connection, but that doesn't help.
You need to set
System.setProperty("sun.net.http.retryPost", "false")
Android’s HTTP Clients
Prior to Froyo,
HttpURLConnection
had some frustrating bugs. In particular, calling close() on a readable InputStream could poison the connection pool. Work around this by disabling connection pooling:Implement a hard timeout yourself, and force close the
HttpURLConnection
by callingdisconnect
. This can be done from yourActivity
usingandroid handler
; If you useAsyncTask
, you can simply callcancel
orThread.interrupt()
:And in your
httpUrlConnTask
, calldisconnect
:You may have to do
urlConnection
in another internal child thread so you can do awhile
loop in asynctask monitoring forisCancelled
. And atry..catch
so you can close all the streams properly.you already have
keepAlive
false, andreadTimeout
, consider addingconnection timeout too
. This will set the socket timeout.For POST calls set
and this should fix the silent retries. The bug report and workaround can be found here.