Search code examples
javaapache-httpclient-4.xhttp-response-codes

How do I get the response code on an HttpClient request


Using the Apache http commons code, the call:

CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);

returns a response object which has a response.getStatusLine() method that returns a String that includes the response code. But no method to get the response code as an int.

How can I get the response code? Parsing the string strikes me as fragile as there might be a message with other numbers in it.

thanks - dave


Solution

  • From the StatusLine, you can call getStatusCode():

    int statusCode = response.getStatusLine().getStatusCode();