How do I get the response code on an HttpClient request

2.8k views Asked by At

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

2

There are 2 answers

2
M A On BEST ANSWER

From the StatusLine, you can call getStatusCode():

int statusCode = response.getStatusLine().getStatusCode();
0
Oliver Tazl On

I think this is what you are looking for:

response.getStatusLine().getStatusCode()

https://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/index.html