HTTP响应代码:400用于URL?

时间:2016-06-26 21:56:30

标签: java web-services post

我无法使用Java请求POST服务。当我在终端上使用curl时,它可以工作:

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

但是,在我的java程序中,它不起作用:

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();

我得到了:

  

- - - -   ----- 400错误请求   -----

错误请求

  -----

浏览器(或代理)发送了该服务器无法理解的请求。

1 个答案:

答案 0 :(得分:0)

再次查看我的问题后,我意识到内容类型应为 application / json ,我发送参数的方式可能会更改为JSON格式:

def split(in_list, count), do: split(in_list, count, 1)

    # Clause 1
    def split(in_list=[head | tail], count, iteration) when count > 0 do
      offset = String.duplicate " ", 5 * (iteration - 1)
      IO.puts offset <> "> FIRST STAGE of CLAUSE 1 / ITERATION #{inspect iteration} called as: split( #{inspect in_list}, #{inspect(count)} ):"
      IO.puts offset <> "  Got 'head'=#{inspect head}, 'tail'=#{inspect tail}, 'count'=#{inspect count}"
      if (count - 1) > 0 do
        IO.puts offset <> "  now I'm going to iterate passing the tail #{inspect(tail)},"
        IO.puts offset <> "  Clause 1 will match as the counter is still > 0"
      else
        IO.puts offset <> "  Now the counter is 0 so I've reached the split point,"
        IO.puts offset <> "  and the Clause 2 instead of Clause 1 will match at the next iteration"
      end

      result = split(tail, count-1, iteration + 1)

      IO.puts offset <> "< Im BACK to the SECOND STAGE of ITERATION #{inspect(iteration)}"
      if (count - 1) == 0 do
        IO.puts offset <> "  got result from CLAUSE 2: #{inspect result}"
      else
        IO.puts offset <> "  got result from previous Clause 1 / Iteration #{iteration + 1}, : #{inspect result}"
      end
      IO.puts offset <> "  {left, right} = #{inspect result}"
      {left, right} = result
      IO.puts offset <> "  Now I'm build the return value as {[head | left], right},"
      IO.puts offset <> "  prepending 'head' (now is #{inspect head}) to the previous value"
      IO.puts offset <> "  of 'left' (now is #{inspect left}) at each iteration,"
      IO.puts offset <> "  'right' instead is always #{inspect right}."
      return = {[head | left], right}
      if (iteration > 1) do
        IO.puts offset <> "  So I'm returning #{inspect return} to iteration #{inspect(iteration - 1)}"
      else
        IO.puts offset <> "  And my final return is at least: #{inspect return} "
      end
      return
    end

    # Clause 2
    def split(list, _count, _iteration) do
      IO.puts ""
      IO.puts "> Greetings from CLAUSE 2 :-), got #{inspect(list)}, returning #{inspect({[], list})}"
      IO.puts ""
      {[], list}
    end

这解决了我的问题。