HttpURLConnection错误 - java.io.IOException:服务器返回HTTP响应代码:400用于URL

时间:2013-08-26 07:32:23

标签: java rest httpurlconnection payload

我正在尝试从桌面应用连接到网址,我收到了问题标题中指出的错误

网址:https://capi-eval.signnow.com/api/user

代码片段。

    public void getUrl(String urlString) throws Exception
            {
            String postData=String.format("{{\"first_name\":\"%s\",\"last_name\":\"%s\",\"email\":\"%s\",\"password\":\"%s\"}}", "FIRST", "LAST","test@test.com","USER_1_PASSWORD");
          URL url = new URL (urlString);
          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
          connection.setDoOutput(true);
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
          connection.setRequestProperty("Accept", "application/json");
          connection.setRequestProperty("Content-Length",""+postData.length());
          connection.setRequestProperty  ("Authorization", "Basic MGZjY2RiYzczNTgxY2EwZjliZjhjMzc5ZTZhOTY4MTM6MzcxOWExMjRiY2ZjMDNjNTM0ZDRmNWMwNWI1YTE5NmI=");
          connection.setRequestMethod("POST");

          connection.connect();

          byte[] outputBytes =postData.getBytes("UTF-8");

          OutputStream os = connection.getOutputStream();
          os.write(outputBytes);

          os.close();
          InputStream is;
          if (connection.getResponseCode() >= 400) {
              is = connection.getErrorStream();
          } else {
              is = connection.getInputStream();
          }
          BufferedReader in   =
                new BufferedReader (new InputStreamReader (is));
          String line;
          while ((line = in.readLine()) != null) {
                System.out.println (line);
                }
}

public static void main(String[] arg) throws Exception
    {

        System.setProperty("jsse.enableSNIExtension", "false");

        Test s = new Test();
        s.getUrl("https://capi-eval.signnow.com/api/user");
    }

当我深入了解响应和打印错误时,Stream My输出为::

{"errors":[{"code":65536,"message":"Invalid payload"}]}

任何帮助表示赞赏

谢谢

1 个答案:

答案 0 :(得分:0)

也许因为没有编码而导致错误。只需尝试使用Base64.encode。