Apache HttpClient执行是否将对所有HTTP 5XX错误抛出IOException? [英] Will Apache HttpClient execute throw an IOException on ALL HTTP 5XX errors?
问题描述
try {
response = client.execute(httpHost, request);
} catch (IOException e) {
// throw custom Exception
} finally {
// close response and client
}
我问的原因是,在此逻辑下,我们正在执行以下操作:
if (response.getStatusLine().getStatusCode() >= 500) {
// Could we ever reach this point after the code above?
}
否, HttpClient 不会为任何 500/5xx响应抛出IOException .
当低级连接失败(例如,无效的主机名,没有服务器监听)或TCP管道异常断开(例如,互联网连接丢失)时,IOException只会发生. >
"HTTP 500"是服务器响应,即 valid 服务器响应,用于指示错误情况.它具有状态码,标头和正文,这是200个响应所具有的全部内容.
文档说返回值是"对请求的[最终]响应";只要服务器能够返回 a 有效响应,就不管状态码如何都是如此.
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?
}
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.
这篇关于Apache HttpClient执行是否将对所有HTTP 5XX错误抛出IOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!