Skip to content
Advertisement

Exception : java.lang.IllegalStateException: Already connected

Hi I’m trying to use HttpURLConnection to send some POST data to a server using the HttpURLConnection class as shown below but it is throwing “java.lang.IllegalStateException: Already connected.” at the code – conn.setRequestProperty(eachEntry.getKey(), eachEntry.getValue()); in the below code. Can any one pls suggest what mistake I am doing here. Also the similar .setRequestPorperty, I am using in other places and it is fine but only when iterating list of request property inside the loop, facing this issue as here.

JavaScript

Advertisement

Answer

I believe the problem is a result of the following log call:

JavaScript

HttpURLConnection#getHeaderFields is inherited from URLConnection which describes the method as returning the response headers (see here).

Therefore, I assume when you call conn.getHeaderFields(), the HttpURLConnection connects and returns you the response header fields. This stops you from being able to edit the request properties of the connection as the connection has already been established – resulting in the error you are experiencing.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement