11

I'm trying to use HttpURLClient to send some POST data to a server using the HttpRestClient class shown below. When executing

conn.setDoInput(true);

I get

java.lang.IllegalStateException: Already connected

I uninstalled the app, and still get the same error.

In all the example I've seen openConnection is called before setDoInput. If, as its name suggests, openConnection opens a connection, it should never be used before `setDoInput, right? What am I missing?

Maybe at some point it crashed before executing disconnect. Could that be the reason? If so, how can I disconnect the old connection?

public class HttpRestClient {
static public int post(String urlStr, List<NameValuePair> data){

    HttpURLConnection conn = null;

    try {

        URL url = new URL(urlStr);


        conn = (HttpURLConnection) url.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.setRequestMethod("POST");

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(data));
        writer.flush();
        writer.close();
        os.close();

        InputStream is = conn.getInputStream();

        String dude = readIt(is);

        return 1;

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 0;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 0;
    }
    finally {
        if(conn!=null) conn.disconnect();
    }
}
}
Svetlin Mladenov
  • 4,307
  • 24
  • 31
jul
  • 36,404
  • 64
  • 191
  • 318
  • you are connecting to a connection that is not yet disconnected due to the static function you are using – Rod_Algonquin May 19 '14 at 14:57
  • 1
    try to remove `conn.connect();` completely (you don't need it). Still got the problem? If yes, then please edit your post and show us the stacktrace. – hgoebl May 19 '14 at 15:19
  • 1
    @Rod_Algonquin I can see no problem regarding `static`. – hgoebl May 19 '14 at 15:43
  • 1
    @hgoebl thanks, I removed the useless `conn.connect()` – jul May 19 '14 at 15:52
  • @Rod_Algonquin it seems not to be a problem: I tried to execute several requests at the same time using my static function and it worked, so I guess it opens a different connection every time. – jul May 19 '14 at 16:00
  • @jul 'at the same time' is not even physically possible ;) did you use it an a multithreaded environment? – Gewure Jun 20 '17 at 09:28

3 Answers3

13

This might be due to watches while debugging in your IDE. See this answer. It happened to me and was hard to discover.

Community
  • 1
  • 1
Sharcoux
  • 5,546
  • 7
  • 45
  • 78
4

You called both of conn.setDoInput(true); and conn.setDoOutput(true);. Use one of them:

  • setDoOutput(true) is used for POST and PUT requests.
  • setDoInput(true) is used for GET request.

The connection you made was confused, it can't decide which request should be used.

In your code:

static public int post(String urlStr, List<NameValuePair> data){
    HttpURLConnection conn = null;
    System.setProperty("http.keepAlive", "false"); // must be set
    try {
        ...
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        // and connect to server, if needed
        conn.connect();
        ...
    }
....
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
2

It may be a misleading exception. See this defect for Jersey-2 https://java.net/jira/browse/JERSEY-2729

The link has been updated: https://github.com/javaee/jersey/issues/3001

Basically the issue is jersey was throwing invalid exception. The real issue in my case was that the connection was refused from the server.

roneo
  • 1,179
  • 13
  • 22