Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HttpClient returning status code and response body?

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!

like image 585
IAmYourFaja Avatar asked Dec 24 '12 18:12

IAmYourFaja


People also ask

How do I get HttpClient response code?

HttpClient client = new DefaultHttpClient(); HttpGet response = new HttpGet("http://www.example.com"); ResponseHandler<String> handler = new BasicResponseHandler(); String body = client. execute(response, handler);

How do you get a response body from Clienthttpresponse?

To get the response body as a string we can use the EntityUtils. toString() method. This method read the content of an HttpEntity object content and return it as a string. The content will be converted using the character set from the entity object.

How do I get response status code from Fetch?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error.

How do I set HTTP status code in response?

To set a different HTTP status code from your Servlet, call the following method on the HttpServletResponse object passed in to your server: res. setStatus(nnn); where nnn is a valid HTTP status code.


2 Answers

Don't provide the handler to execute.

Get the HttpResponse object, use the handler to get the body and get the status code from it directly

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {     final HttpGet httpGet = new HttpGet(GET_URL);      try (CloseableHttpResponse response = httpClient.execute(httpGet)) {         StatusLine statusLine = response.getStatusLine();         System.out.println(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());         String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);         System.out.println("Response body: " + responseBody);     } } 

For quick single calls, the fluent API is useful:

Response response = Request.Get(uri)         .connectTimeout(MILLIS_ONE_SECOND)         .socketTimeout(MILLIS_ONE_SECOND)         .execute(); HttpResponse httpResponse = response.returnResponse(); StatusLine statusLine = httpResponse.getStatusLine(); 

For older versions of java or httpcomponents, the code might look different.

like image 67
Aviram Segal Avatar answered Sep 19 '22 14:09

Aviram Segal


You can avoid the BasicResponseHandler, but use the HttpResponse itself to get both status and response as a String.

HttpResponse response = httpClient.execute(get);  // Getting the status code. int statusCode = response.getStatusLine().getStatusCode();  // Getting the response body. String responseBody = EntityUtils.toString(response.getEntity()); 
like image 45
lkamal Avatar answered Sep 17 '22 14:09

lkamal