Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How get the status-code of an HttpClient request

I want to download a file and need to check the response status code (ie HTTP /1.1 200 OK). This is a snipped of my code:

HttpGet httpRequest = new HttpGet(myUri); HttpEntity httpEntity = null; HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httpRequest); ... 

How do i get the status-code of the response?

like image 652
whlk Avatar asked Apr 07 '10 13:04

whlk


People also ask

How do I get HttpClient status code?

Try with: GetMethod getMethod = new GetMethod("http://www.example.com"); int res = client. executeMethod(getMethod); This should do the trick!

What HTTP status code do we get a client side error?

A first digit of 4 represents a client—side error, with the most common codes in the range of 400 to 404. A first digit of 5 represents a server—side error, with the most common codes in the range of 500 to 510. Because the codes in 400 and 500 range represent errors, they are also referred to as HTTP Error Codes.

What is request status code?

An HTTP status code is a server response to a browser's request. When you visit a website, your browser sends a request to the site's server, and the server then responds to the browser's request with a three-digit code: the HTTP status code.


2 Answers

This will return the int value:

response.getStatusLine().getStatusCode() 
like image 108
Robby Pond Avatar answered Oct 20 '22 20:10

Robby Pond


if (response.getStatusLine().getStatusCode()== HttpsURLConnection.HTTP_OK){ ... } 
like image 37
NickUnuchek Avatar answered Oct 20 '22 18:10

NickUnuchek