Apache HttpClient 执行会在所有 HTTP 5XX 错误上抛出 IOException 吗? [英] Will Apache HttpClient execute throw an IOException on ALL HTTP 5XX errors?
问题描述
Apache HttpClient 文档 用于 execute(HttpHost target, HttpRequest request)
方法说:
IOException - 如果出现问题或连接中止
IOException - in case of a problem or the connection was aborted
如果我捕获 IOException,这会捕获 ALL 服务器 5xx 错误?
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?
}
推荐答案
不,HttpClient 不会抛出一个 IOException for any 500/5xx 响应强>.
No, HttpClient will not throw an IOException for any 500/5xx response.
IOException仅在低级连接失败(例如无效的主机名、没有服务器侦听)或 TCP 管道异常断开(例如互联网连接丢失)时发生.
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).
HTTP 500"是服务器响应 - 有效 服务器响应 - 用于指示错误情况.它有一个状态代码、标题和正文,这是 200 响应所具有的一切.
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屋!