HttpClient获取状态代码 [英] HttpClient get status code

查看:489
本文介绍了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);

如何从<提取状态代码(202,404等) code>正文?或者,如果在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获取状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆