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?
Try with: GetMethod getMethod = new GetMethod("http://www.example.com"); int res = client. executeMethod(getMethod); This should do the trick!
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.
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.
This will return the int
value:
response.getStatusLine().getStatusCode()
if (response.getStatusLine().getStatusCode()== HttpsURLConnection.HTTP_OK){ ... }
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