簡體   English   中英

HttpURLConnection錯誤-java.io.IOException:服務器返回的HTTP響應代碼:URL的400

[英]HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL

我正在嘗試從桌面應用程序連接到URL,但出現問題標題中指出的錯誤

網址: 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我的輸出是::

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

任何幫助表示贊賞

謝謝

可能是由於未編碼引起的問題。 只需嘗試使用Base64.encode。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM