本文介绍了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);
如何从正文?或者,如果在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
)
(我读得太快了!)
试用:
GetMethod getMethod = new GetMethod("http://www.example.com");
int res = client.executeMethod(getMethod);
这应该可以解决问题!
这篇关于HttpClient获取状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!