1

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

Blo
  • 11,903
  • 5
  • 45
  • 99
Arutha
  • 26,088
  • 26
  • 67
  • 80

1 Answers1

1

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 Orr
  • 110,418
  • 27
  • 198
  • 193
  • The documentation for the disconnect method explicitly says that you should not try to reuse the connection afterward. – Nate C-K Oct 12 '11 at 16:52
  • @Nate C-K Yes, I advocating creating a new connection. Maybe you meant to post this as an answer, or as a comment on the question?? – Christopher Orr Oct 13 '11 at 13:56