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

女生的网名这么多〃 提交于 2019-12-23 12:07:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!