如果 getResponseCode() == 404 并因此 getInputStream() 会抛出异常,有没有办法读取如下所示的简单 http-get 的响应?如果可能的话,我更愿意留在 java.net。
HttpURLConnection c = (HttpURLConnection)new URL("http://somesite.com").openConnection;
c.openInputStream();
Run Code Online (Sandbox Code Playgroud)
问题是,我(确实)有一个我想用 java 读取的网站,该网站响应 404,但显示在浏览器中,因为它显然无论如何都会有响应。
如果getErrorStream().getResponseCode() == 404
HttpURLConnection c = (HttpURLConnection)new URL("http://somesite.com").openConnection();
InputStream in = null;
if (c.getResponseCode() >= 400) {
in = c.getErrorStream();
}
else {
in = c.getInputStream();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1897 次 |
| 最近记录: |