Java won't POST to webserver

So for about an hour I have tried to get my java method to post to my webserver, but for some reason it refuses to. The get’s arrives just fine, but for some reason not the posts.

public static void refresh(){
        LEDS[1].setState(LEDS[1].getState().equals(HIGH) ? LOW : HIGH);
        final String data = "updatesPerSecond=" + Integer.valueOf(PinTest.processCountPerSecond);
        final String URL = "http://127.0.0.1/light/index.php?"+data;

        try{
            HttpURLConnection conn = (HttpURLConnection)new URL(URL).openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty( "charset", "utf-8");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            
            //Send data
            try(DataOutputStream out = new DataOutputStream(conn.getOutputStream()); InputStream in = conn.getInputStream()){
                //Send data
                out.writeBytes(data);
                out.flush();
                
                //Read reply
                int lenght;
                byte[] buffer = new byte[1024];
                while((lenght = in.read(buffer)) != -1){
                    in.read(buffer, 0, lenght);
                }
            }
        }catch(Exception e){
            LEDS[0].setState(HIGH);
        }
    }

I know the read does nothing, I just attempted everything I can think of.

Can your server receive a curl post?


curl --data "param1=value1&param2=value2" http://hostname/resource

Some potential problems I can think of:

In the following line you try to open the output and input streams simultaneously. If I remember correctly the request is sent when you call conn.getInputStream, which would mean your content body is never sent:

    try(DataOutputStream out = new DataOutputStream(conn.getOutputStream()); InputStream in = conn.getInputStream()){ 

Maybe you get an exception and the LEDS thing does not work so it goes unnoticed? Can you log or print the exception?

        }catch(Exception e){
            LEDS[0].setState(HIGH);
        }

Just maybe, but this may be wrong, calling doInput after doOutput resets doOutput? I know that doInput(true) does not need to be called since it’s the default:

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