Reputation:
Is there a way to read a response of a simple http-get made like below, if getResponseCode() == 404 and thus getInputStream() would throw an exception? I'd prefer to stay with java.net, if it's possible.
HttpURLConnection c = (HttpURLConnection)new URL("http://somesite.com").openConnection;
c.openInputStream();
Thing is, that I have (indeed) a site I want to read out with java that responds with 404, but displays in a browser, because it obviously caries a response anyway.
Upvotes: 3
Views: 2178
Reputation: 143906
You want to use the getErrorStream()
method if the 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();
}
Upvotes: 6