0

I send a post to a mail api like this:

HttpURLConnection conn = (HttpURLConnection)MAIL_SERVER_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

OutputStream os = conn.getOutputStream();
os.write(toSend.build().toString().getBytes("UTF-8"));
os.flush();
os.close();


System.out.println(String.format("Failed : HTTP error code[%s] msa[%s]",conn.getResponseCode(), conn.getResponseMessage()));  


InputStreamReader rd = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(rd);

String output;
System.out.println("Output from Server .... \n");
   while ((output = br.readLine()) != null) {
        System.out.println(output);
}

conn.disconnect();

The api give me a response like this: enter image description here

I don't know how read, using Java, the message in the body of the response.
This line new InputStreamReader(conn.getInputStream()); always generate ad exception like this:

Severe:   java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:8182/mailapi/highpriority
user72708
  • 1,235
  • 3
  • 13
  • 20

0 Answers0