views:

429

answers:

2

The method disconnect from HttpURLConnection seems not to work properly. If I execute the following code:

url = new URL ("http:// ...");
connection = (HttpURLConnection) url.openConnection ();
connection.setRequestMethod ("POST");
connection.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect ();
// Some code
connection.disconnect ();
connection.setDoInput(false); // -> IllegalStateException

I get an IllegalStateException when I call the method setDoInput. The exception says: "Already connected"

+1  A: 

Try using this pattern instead for HTTP POST operations with HttpUrlConnection.

CommonsWare
Unfortunately, I can not because the code comes from a Java library and I can not change the code.
Arutha
A: 

It sounds like you're trying to reuse the connection? i.e. altering the request properties after you've disconnected from the server, ready to make another connection.

If that is the case, then just create a new HttpURLConnection object.

Christopher
Yet it works perfectly with the J2SE api...
Arutha