Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can't set properties to an request using HttpURLConnection

843802Aug 16 2007
I try to connect to an web application with my JWS application.
I have to use session tracking and therefore I have to read and write the
SessionId.
Every time I try to set the cookie for SessionId get this Exception:
java.lang.IllegalStateException: Already connected
        at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:2095)
This is my code:
        try
        {
            URL url = new URL( "http:\\www....." );

            conn                    = ( HttpURLConnection ) url.openConnection();

            //!!!!!!!!!!!!!!!!!!!!!!!!!! this is line throws the exception!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
            conn.setRequestProperty( "Cookie", "JSESSIONID=aswx12ff3G222c_f" );
            conn.connect();

            BufferedInputStream inBuf      = new BufferedInputStream( conn.getInputStream() );
            InputStreamReader   inStReader = new InputStreamReader( inBuf, "ISO-8859-1" );

            char[]              buffer     = new char[ 1024 ];
            int                 nbytes;

            // Read and write the Data
            while ( ( nbytes = inStReader.read( buffer ) ) != -1 )
            {
                strReturn = strReturn.concat( String.valueOf( buffer, 0, nbytes ) );
            }
        }
        catch ( MalformedURLException e )
        {
            e.printStackTrace();
        }
        catch ( IOException ex )
        {
            ex.printStackTrace();
        }
        finally
        {
            conn.disconnect();
        }
        return strReturn;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 13 2007
Added on Aug 16 2007
0 comments
285 views