HttpsURLConnection Already connected when trying to add cookies

I get an exception with this stack trace

Exception in thread "main" java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2410)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:296)
at com.hello.world.CookieManager.setCookies(CookieManager.java:124)
at com.hello.world.HttpHandler.PostHttps(HttpHandler.java:101)

These are the lines in HttpHandler.PostHttps

URL url = new URL("https://www.example.com/");
HttpsURLConnection httpsConn = (HttpsURLConnection)url.openConnection();
cm.setCookies(httpsConn);

And these are those contained in the cookieManager.setCookies

String cookieStr = "name=value";
httpsConn.setRequestProperty("Cookie", cookieStr);

I know that I cannot set the cookie header while the connection is open. But I don’t call anything on the connection object except initializing the object using openConnection () and then adding cookies.

I also know that calling some other methods, such as getHeaderFiles or starting a thread, will open the connection and there will be no call to such a method.

Edit:

I used JDK 1.6.0.39. I downloaded the latest version 1.7.0.51 and it has been fixed. Turns it was a version issue.

Edit 2:

He appeared again, strange, I must add.

+3
source share

All Articles