Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient get status code [duplicate]

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!

like image 465
IAmYourFaja Avatar asked Jun 06 '12 10:06

IAmYourFaja


People also ask

What is the HTTP status code for duplicate record?

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.

What HTTP status code for user already exists?

The appropriate status code for "Already Exists" would be '409 Conflict'.

What is a 201 response code?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

What does code 303 mean?

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.


1 Answers

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!

like image 113
Enrichman Avatar answered Sep 29 '22 01:09

Enrichman