Some time ago I've posted this bug report:
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8024810
and I'm quite frustrated with the comment I received.
I don't know if the person who posted it knows:
- that I have no way to replay to him there
- I have no info who he is so I have no way of contacting him personalty
- what the javadoc of this method says
?
------------------------------------------------
Quote: http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#getResponseCode()
public int getResponseCode() throws IOException
Gets the status code from an HTTP response message. For example, in the case of the following status lines:
HTTP/1.0 200 OK
HTTP/1.0 401 Unauthorized
It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).
Returns:
the HTTP Status-Code, or -1
Throws:
IOException - if an error occurred connecting to the server.
----------------------------------------------
How on earth can one come to the conclusion that throwing an IOException is the correct behavior in getResponseCode() if the server returned a 4xx or 5xx code ?!
As the javadoc states the IOException should only be thrown if an error occurred connecting to the server. The connection was established. The conversation was done according to the HTTP protocol. This method should return the error code which the server send to the client. This is not an connection error. This a response from a server to the request. If the server responds with the 401 code then (as the javadoc says) I MUST get that error code from this method NOT an IOException. There is no reason for this method to even exist if it can only return 2xx HTTP codes ...