问题
The Apache HttpClient docs for the execute(HttpHost target, HttpRequest request)
method says:
IOException - in case of a problem or the connection was aborted
If I catch the IOException, will this catch ALL Server 5xx Errors?
try {
response = client.execute(httpHost, request);
} catch (IOException e) {
// throw custom Exception
} finally {
// close response and client
}
The reason I'm asking is that after this logic somewhere else down the line we're doing something like the following:
if (response.getStatusLine().getStatusCode() >= 500) {
// Could we ever reach this point after the code above?
}
回答1:
No, HttpClient will not throw an IOException for any 500/5xx response.
An IOException occurs only when the low-level connection failed (eg. invalid hostname, no server listening) or the TCP pipe was abnormally broken (eg. internet connection was lost).
An 'HTTP 500' is a server response - a valid server response - to indicate an error condition. It has a status code, headers, and body, which is everything a 200 response has.
The documentation says the return value is "the [final] response to the request"; this is true regardless of the status code as long as the server was able to return a valid response.
来源:https://stackoverflow.com/questions/32434947/will-apache-httpclient-execute-throw-an-ioexception-on-all-http-5xx-errors