我正在尝试使用Apache HttpClient发出HTTP请求,然后显示HTTP响应代码(200、404、500等)以及HTTP响应正文(文本字符串)。需要注意的是,我正在使用v4.2.2
,因为大多数HttpClient示例都来自v.3.x.x
,而从版本3到版本4的API发生了很大的变化。
不幸的是,我只能让HttpClient返回状态码或响应正文(但不能同时返回两者)。
这是我的代码:
// Getting the status code.
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://whatever.blah.com");
HttpResponse resp = client.execute(httpGet);
int statusCode = resp.getStatusLine().getStatusCode();
// Getting the response body.
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://whatever.blah.com");
ResponseHandler<String> handler = new BasicResponseHandler();
String body = client.execute(httpGet, handler);
所以我问:使用v4.2.2
库,如何从同一client.execute(...)
调用中获取状态码和响应体?
whatever.blah.com
服务器)。 - SJuan76