momal 0 Newbie Poster

I have a localhost server running on port 4000 which listens to requests sent to it and executes commands and returns output to client in a json format.

I'm actually trying to run docker commands for the gcc compiler image.

I have a code in PHP that is working just fine. It is the following:

protected function HTTPRequest($url, $command){
        //open connection
        $ch = curl_init();
        $fields['command'] = $command;
        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //execute post
        $result = curl_exec($ch);
        //close connection
        curl_close($ch);
        return $result;
    }

<p> the url here is

"http://localhost:4000"

and the command is

docker run --rm -v ~/test.c:/usr/src/test.c gcc:4.9 /bin/bash -c 'gcc -o /usr/src/myapp /usr/src/test.c && /usr/src/./myapp'

This works absolutely fine. But I need this to be run from a jsp page. So I found the equivalent of this whole. I tried it, put in the url and the command but it does nothing.

Can anyone tell what's wrong? I'm a newbie at this and have no idea what's going on.

public String sendData() throws IOException {
        // curl_init and url
        URL url = new URL("http://localhost:4000");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        //  CURLOPT_POST
        con.setRequestMethod("POST");

        // CURLOPT_FOLLOWLOCATION
        con.setInstanceFollowRedirects(true);

        String postData = "docker run --rm -v ~/test.c:/usr/src/test.c gcc:4.9 /bin/bash -c 'gcc -o /usr/src/myapp /usr/src/test.c && /usr/src/./myapp'";
        con.setRequestProperty("Content-length", String.valueOf(postData.length()));

        con.setDoOutput(true);
        con.setDoInput(true);

        DataOutputStream output = new DataOutputStream(con.getOutputStream());
        output.writeBytes(postData);
        output.close();

        // "Post data send ... waiting for reply");
        int code = con.getResponseCode(); // 200 = HTTP_OK
        System.out.println("Response    (Code):" + code);
        System.out.println("Response (Message):" + con.getResponseMessage());

        // read the response
        DataInputStream input = new DataInputStream(con.getInputStream());
        int c;
        StringBuilder resultBuf = new StringBuilder();
        while ( (c = input.read()) != -1) {
            resultBuf.append((char) c);
        }
        input.close();

        return resultBuf.toString();
    }