HttpClient 获取状态码 [英] HttpClient get status code
本文介绍了HttpClient 获取状态码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Apache HttpClient 4.1.3 并尝试从 HttpGet
获取状态代码:
Using Apache HttpClient 4.1.3 and trying to get the status code from an HttpGet
:
HttpClient client = new DefaultHttpClient();
HttpGet response = new HttpGet("http://www.example.com");
ResponseHandler<String> handler = new BasicResponseHandler();
String body = client.execute(response, handler);
如何从 body
中提取状态代码(202、404 等)? 或者,如果在 4.1.3 中有另一种方法可以做到这一点,是什么?
How do I extract the status code (202, 404, etc.) from the body
? Or, if there's another way to do this in 4.1.3, what is it?
此外,我认为完美/良好的 HTTP 响应是 HttpStatus.SC_ACCEPTED
,但也希望对此进行确认.提前致谢!
Also, I assume a perfect/good HTTP response is an HttpStatus.SC_ACCEPTED
but would like confirmation on that as well. Thanks in advance!
推荐答案
尝试:
HttpResponse httpResp = client.execute(response);
int code = httpResp.getStatusLine().getStatusCode();
HttpStatus 应该是 200 ( HttpStatus.SC_OK
)
The HttpStatus should be 200 ( HttpStatus.SC_OK
)
(这个问题我读得太快了!)
(I've read too fast the problem!)
尝试:
GetMethod getMethod = new GetMethod("http://www.example.com");
int res = client.executeMethod(getMethod);
这应该可以解决问题!
这篇关于HttpClient 获取状态码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文