Using Apache HttpClient 4.1.3 and trying to get the status code from an HttpGet
:
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 I extract the status code (202, 404, etc.) from the body
? Or, if there's another way to do this in 4.1.3, what is it?
Also, I assume a perfect/good HTTP response is an HttpStatus.SC_ACCEPTED
but would like confirmation on that as well. Thanks in advance!
409 Conflict - Client attempting to create a duplicate record, which is not allowed. 410 Gone - The requested resource has been deleted. 411 Length Required - The server will not accept the request without the Content-Length Header.
The appropriate status code for "Already Exists" would be '409 Conflict'.
The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.
A 303 redirect is a response to an HTTP status code 303, which is also called a "See Other" status code. Experts describe the specific type of redirect as a response to a request for a Unified Resource Identifier (URI) that identifies a real-world object. A 303 redirect may also be called HTTP 303.
EDIT:
Try with:
HttpResponse httpResp = client.execute(response); int code = httpResp.getStatusLine().getStatusCode();
The HttpStatus should be 200 ( HttpStatus.SC_OK
)
(I've read too fast the problem!)
Try with:
GetMethod getMethod = new GetMethod("http://www.example.com"); int res = client.executeMethod(getMethod);
This should do the trick!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With