Diyarbakir
Diyarbakir

Reputation: 2099

Will Apache HttpClient execute throw an IOException on ALL HTTP 5XX errors?

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?
}

Upvotes: 5

Views: 6548

Answers (1)

user2864740
user2864740

Reputation: 61975

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.

Upvotes: 13

Related Questions