Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 782
David Thielen Avatar asked Apr 15 '26 01:04

David Thielen


2 Answers

From the StatusLine, you can call getStatusCode():

int statusCode = response.getStatusLine().getStatusCode();
like image 83
M A Avatar answered Apr 17 '26 13:04

M A


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

like image 40
Oliver Tazl Avatar answered Apr 17 '26 14:04

Oliver Tazl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!