【问题标题】:Get detailed error message with REST API Azure in Java使用 Java 中的 REST API Azure 获取详细的错误消息
【发布时间】:2014-05-14 10:25:42
【问题描述】:

我正在尝试用 Java 在 Azure 中添加一个 VM,我想知道是否有办法显示完整的错误消息,而不仅仅是状态代码和错误名称?

我使用HttpsURLConnection 类中的getResponseCode()getResponseMessage()。例如我有400Bad request 但不够详细。

编辑:这是我的 Java 代码

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

【问题讨论】:

    标签: java azure virtual-machine error-code


    【解决方案1】:

    并没有真正在 Java 中工作(因此可能有更好的方法),但我在写一篇关于同一主题的博客文章时确实遇到了类似的问题。这就是我发现错误详细信息的方式:

    假设 con 是您的 HttpsURLConnection 对象:

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

    }

    这是我写的一个通用的 post 方法:

    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;
    }
    

    【讨论】:

    • 我编辑了你的帖子。 return 语句丢失。我试试你的代码,但getErrorStream() 返回null
    • 您在代码中的何处添加请求数据?我在回复中添加了我在博客文章中写的通用 Post 方法。 HTH。
    • 我发现了错误。我做了return con.responseCode();。现在,如果我执行int responseCode = con.getResponseCode();,在显示错误之前,errorStream 不为空。非常感谢您的帮助!
    猜你喜欢
    • 2014-10-11
    • 2016-08-14
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2020-04-05
    • 2019-11-12
    • 2016-12-30
    相关资源
    最近更新 更多