HTTP Post请求读取响应

时间:2015-09-17 15:44:49

标签: java rest http-post

我发帖子给这样的邮件api:

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

api给我这样的回复: enter image description here

我不知道如何使用Java读取响应正文中的消息 此行new InputStreamReader(conn.getInputStream());始终生成广告异常,如下所示:

Severe:   java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:8182/mailapi/highpriority

0 个答案:

没有答案