I am trying to get Apache HttpClient to fire an HTTP request, and then display the HTTP response code (200, 404, 500, etc.) as well as the HTTP response body (text string). It is important to note that I am using v4.2.2
because most HttpClient examples out there are from v.3.x.x
and the API changed greatly from version 3 to version 4.
Unfortunately I've only been able to get HttpClient returning the status code or the response body (but not both).
Here's what I have:
// 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);
So I ask: Using the v4.2.2
library, how can I obtain both status code and response body from the same client.execute(...)
call? Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…