在Java中,当HTTP结果为404范围时,此代码抛出异常:
URL url = new URL("http://stackoverflow.com/asdf404notfound");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.getInputStream(); // throws!
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我碰巧知道内容是404,但我仍然想要阅读回复的主体.
(在我的实际情况中,响应代码是403,但响应正文解释了拒绝的原因,我想将其显示给用户.)
我如何访问响应正文?