2013-08-26 73 views
0

我試圖連接到從桌面應用程序的URL,而我得到的標題指出我的問題的錯誤HttpURLConnection的錯誤 - java.io.IOException異常:服務器返回的HTTP響應代碼:400網址

網址: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","[email protected]","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"); 
    } 

當我在響應和打印錯誤挖澗我的輸出爲::

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

任何幫助讚賞

感謝

+0

http://stackoverflow.com/questions/4931164/urlconnection-error-java-io-ioexception-server-returned-http-response-code-4 ||檢查這個,它可能有幫助嗎? – Adly

+0

我已經通過該網址也使用了從那裏本身的代碼。但我得到不同的錯誤。 –

回答

1

或許真的錯了unecoding引起的。只需使用Base64.encode嘗試一下。

相關問題