2014-05-14 3 views
0

Java에서 Azure로 VM을 추가하려고하는데 상태 코드와 오류뿐만 아니라 전체 오류 메시지를 표시하는 방법이 있는지 궁금합니다. 이름?Java에서 REST API Azure로 자세한 오류 메시지 받기

HttpsURLConnection에서 getResponseCode()getResponseMessage()을 사용합니다. 예를 들어 나는 400Bad request이지만 세부 사항은 충분하지 않습니다.

편집 : 여기에 정말 자바에서 일을 안 내 자바 코드

URL url = new URL("https://management.core.windows.net/myID/services/hostedservices/myHostedService/deployments"); 
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 
con.setRequestMethod("POST"); 
con.addRequestProperty("x-ms-version", "2014-05-01"); 
con.setRequestProperty("Content-Type", "application/xml"); 

InputStream errorStream = con.getErrorStream();//Get the error stream 
if (errorStream != null) {//Read the detailed error message from the stream 
    String detailedErrorMessage = getStringFromInputStream(errorStream); 
    System.out.println(detailedErrorMessage); 
} 

답변

1

이다 (그래서 거기를하는 더 나은 방법이 될 수 있습니다)하지만, 동일한 주제에 대한 블로그 게시물에 작업 할 때 나는 비슷한 문제가 발생했다. conHttpsURLConnection 개체입니다

가 가정 :

int responseCode = con.getResponseCode(); 
    InputStream errorStream = con.getErrorStream();//Get the error stream 
    if (errorStream != null) {//Read the detailed error message from the stream 
     String detailedErrorMessage = getStringFromInputStream(errorStream); 
     System.out.println(detailedErrorMessage); 
    } 

여기 getStringFromInputStream 메소드의 구현입니다 :

// Source - http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/ 
private static String getStringFromInputStream(InputStream is) { 

    BufferedReader br = null; 
    StringBuilder sb = new StringBuilder(); 

    String line; 
    try { 

     br = new BufferedReader(new InputStreamReader(is)); 
     while ((line = br.readLine()) != null) { 
      sb.append(line); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (br != null) { 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    return sb.toString(); 

}

가 여기에 일반적인이다 이것은 내가 오류 정보를 발견하는 방법이다 게시물 방법 나는 쓴 :

private static int processPostRequest(URL url, byte[] data, String contentType, String keyStore, String keyStorePassword) throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException, ProtocolException { 
    SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword); 
    HttpsURLConnection con = null; 
    con = (HttpsURLConnection) url.openConnection(); 
    con.setSSLSocketFactory(sslFactory); 
    con.setDoOutput(true); 
    con.setRequestMethod("POST"); 
    con.addRequestProperty("x-ms-version", "2013-08-01"); 
    con.setRequestProperty("Content-Length", String.valueOf(data.length)); 
    con.setRequestProperty("Content-Type", contentType); 

    DataOutputStream requestStream = new DataOutputStream (con.getOutputStream()); 
    requestStream.write(data); 
    requestStream.flush(); 
    requestStream.close(); 
    int responseCode = con.getResponseCode(); 
    InputStream errorStream = con.getErrorStream(); 
    if (errorStream != null) { 
     String detailedErrorMessage = getStringFromInputStream(errorStream); 
     System.out.println(detailedErrorMessage); 
    } 
    return responseCode; 
} 
+0

귀하의 게시물을 편집했습니다. 'return' 문이 없습니다. 당신의 코드를 시도하지만'getErrorStream()''null'을 리턴하십시오 – JGeo

+0

코드를 게시 할 수 있습니까? –

+0

내 게시물을 편집했습니다. – JGeo