如何获取 HttpClient 请求的响应代码

How do I get the response code on an HttpClient request

使用 Apache http 公共代码,调用:

CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);

returns 具有 response.getStatusLine() 方法的响应对象 returns 包含响应代码的字符串。但是没有方法将响应代码作为 int.

如何获取响应代码?解析字符串让我觉得很脆弱,因为其中可能包含其他数字的消息。

谢谢 - 戴夫

StatusLine,您可以调用 getStatusCode():

int statusCode = response.getStatusLine().getStatusCode();

我想这就是您要找的:

response.getStatusLine().getStatusCode()

https://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/index.html