1
HTTP/1.1 500 Internal Server Error
Content-Type: application/xml; charset=UTF-8
Connection: close
Content-Length: 401

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<fulfillmentService><responseCode>1106</responseCode> 
<status>FAILURE</status><systemType>CIS</systemType> 
<responseDescription>Sorry! We are unable to process your request. Call 111 for customer care or visit www.mtnzambia.com.</responseDescription><requestId>WEB_10.100.244.97_92505288-f094-4834-a978-658c19e3f656</requestId></fulfillmentService>

I need a way of logging this XML response in java. When I send a request from my java this is what is returned. I need a way of printing out the response description.

Below is my java code:

 url = new URL(endpoint);


        connection = (HttpURLConnection) url.openConnection();
  responseCode = connection.getResponseCode();
        respTxt = connection.getResponseMessage();
        System.out.println("Status code after sending the the payload to mtn => " + responseCode + " Now waiting for response: ");
        Object vm = connection.toString();
        //   BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        PrintWriter pw = new PrintWriter(connection.getOutputStream());

        // send xml to jsp
        // pw.write(buffer, 0, bytes_read);
        // pw.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }
MLavoie
  • 9,671
  • 41
  • 36
  • 56

1 Answers1

2

If the response code isn't 200 or 2xx or 3xx, use getErrorStream() instead of getInputStream().

Please see How to get response body using HttpURLConnection, when code other than 2xx is returned?

    url = new URL(endpoint);

            connection = (HttpURLConnection) url.openConnection();
            responseCode = connection.getResponseCode();
            respTxt = connection.getResponseMessage();
            System.out.println("Status code after sending the the payload to mtn => " + responseCode + " Now waiting for response: ");
            Object vm = connection.toString();
            //   BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            //PrintWriter pw = new PrintWriter(connection.getOutputStream());

            // send xml to jsp
            // pw.write(buffer, 0, bytes_read);
            // pw.close();
            BufferedReader in;

            if(responseCode >= 200 && responseCode  < 400) {
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            } else {
                in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            }
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
John
  • 446
  • 6
  • 16