Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:47:33+00:00 2026-05-20T16:47:33+00:00

This question seems clear what is happening: I already have a connection open, the

  • 0

This question seems clear what is happening: I already have a connection open, the problem is I don’t know why.

Right now I’m just testing my http login and out.

Login:

HttpURLConnection con = openConnection(URLGenerator.getLoginURL(), true, true,"POST");
        String content = ...;
        writeToOutput(con, content);
        con.connect();
        String cookieVal = con.getHeaderField("Set-Cookie");
        if(cookieVal != null)
        {
            sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
        }
        con.disconnect();
        return con.getResponseCode();

Logout:

    HttpURLConnection con = openConnection(URLGenerator.getLogoutURL(), true, true,"GET");
    String content = ...;
    writeToOutput(con, content);
    setCookies(con);
    con.connect();
    con.disconnect();
    return con.getResponseCode();

and for the code lovers the function OpenConnection (because I know the first thing people are going to ask is “where is this function”

public static final HttpURLConnection openConnection(URL url, boolean in, boolean out,String requestMethode) throws IOException{
        HttpURLConnection con = (HttpURLConnection) url.openConnection ();
        con.setDoInput(in);
        con.setDoOutput (out);
        if(requestMethode == null){
            requestMethode = "GET";
        }
        con.setRequestMethod(requestMethode);
        con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
        return con;
    }

This is the function that triggers the java.lang.IllegalStateException: Already connected

public static final void setCookies(HttpURLConnection con){
        if(sessionId != null)
        {
            con.setRequestProperty("Cookie", sessionId);
        }
    }

What I don’t get is why the connection is still open.

I even tried to call disconnect but it doesn’t work.

I thought the principal was to set up a HttpURLConncetion object and do connect to execute the request, receive the result and that terminated the connection.

Any ideas?

Thanks Jason

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:47:34+00:00Added an answer on May 20, 2026 at 4:47 pm

    You need to set cookies (request header values) before you write to the request body. You can’t change the request headers anymore when the first bit of the request body is been sent.

    So, change

    writeToOutput(con, content);
    setCookies(con);
    

    to

    setCookies(con);
    writeToOutput(con, content);
    

    and this problem should disappear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a basic question, but I haven't found any clear answers. Essentially,
This question seems to have been asked a lot, but I haven't seen an
I know this question has been asked quite often, but this seems to be
I know this question may have appeared few times here and in the internet.
This question seems to suggest that Ajax requests are not guaranteed to return in
This question seems to be the essence of several others on this forum. I
This question seems to be asked freqeuently over the internet but I still can't
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this question seems a bit complex but I think its all related
After ages of programming in php this question seems really weird still to me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.