Question

I'm trying to add a VM in Azure in Java and I'm wondering if there is to way to display the full error message, not only the status code and the error name ?

I use getResponseCode() and getResponseMessage() from the class HttpsURLConnection. For example I have 400 and Bad request but it's not enought detailed.

EDIT : here's my Java code

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);
}
Was it helpful?

Solution

Not really doing work in Java (so there may be better ways of doing it) but I did encounter similar issue when working on a blog post on same topic. This is how I found out error details:

Assuming con is your HttpsURLConnection object:

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

and here's the implementation of getStringFromInputStream method:

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

}

Here's a generic post method I wrote:

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top