I want to use HttpURLConnection
to connect to my webservice, POST an XML and get a result. I am using the following code:
URL url = new URL(urlString);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
Problem is, when I call the setRequestProperty
method, it fails with an IllegalStateException
saying I am "already connected". Apparently, openConnection
in fact opens the connection to the URL (in my debugger, I can see the connected
boolean set to true
). According to the URL
documentation of Oracle though, it shouldn't. The Android docs are unclear about it.
How do I prevent openConnection
to connect, so I can set extra properties?
Update it looks like the connection
is in some pool and doesn't get disconnected, not even after calling connection.disconnect()
, or even killing the server.
I do not think this is a duplicate of this question as it gives no real answer. Also, the documentation seems to be unclear.
openConnection()
does not connect, and the code you have posted does not behave as yu have described. What opens the TCP connection is any of the following:getInputStream()
getErrorStream()
getResponseCode()
Ergo you must have called one of those before trying to set a request property.