HTTP response code: 400 for URL?
<p>I am not able to request a POST service using Java. When I use curl on a terminal it works:</p>

<pre><code>curl -i -H "Content-Type: application/json" -X POST -d '{"blackoutDate":"2016/05/03", "blackoutTime":"18:45:36"}' http://poweranalyzer-skyglover.rhcloud.com/blackouts/
</code></pre>

<p>However, on my java program it does not work:</p>

<pre><code>String uri = "http://poweranalyzer-skyglover.rhcloud.com/blackouts/";
        URL url = new URL(uri);
        StringBuffer params = new StringBuffer("");
        params.append("blackoutDate=" + URLEncoder.encode("2016/05/03", "UTF-8"));
        params.append("blackoutTime=" + URLEncoder.encode("18:45:36", "UTF-8"));

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", String.valueOf(params.toString().length()));
        connection.connect();

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(params.toString());
        writer.flush();

        InputStream inputStream;
        if (connection.getResponseCode() == 201) {
            inputStream = connection.getInputStream();
        } else {
            inputStream = connection.getErrorStream();
        }

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = rd.readLine()) != null)
        {
            System.out.println("-----" + line);
        }
        writer.close();
        rd.close();

        connection.disconnect();
</code></pre>

<p>I am getting:</p>

<blockquote>
  <p>-----
  -----400 Bad Request
  -----<h1>Bad Request</h1>
  -----<p>The browser (or proxy) sent a request that this server could not understand.</p></p>
</blockquote>