Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUrlConnectionDelegate - Getting http status codes

in iOS, how can I receive the http status code (404,500 200 etc) for a response from a web server. I am assuming it's in the NSUrlConnectionDelegate.

Objective-C or Monotouch .NET answer ok.

like image 897
Ian Vink Avatar asked Aug 02 '11 21:08

Ian Vink


People also ask

How can I get HTTP status code?

Just use Chrome browser. Hit F12 to get developer tools and look at the network tab. Shows you all status codes, whether page was from cache etc.

How do I get my HTTP status code 201 back?

2 201 Created. The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field.

How do I get my HTTP status code 200?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.

What is HTTP status code000?

000 is a common code to use when no HTTP code was received due to a network error. According to a knowledge base article for Amazon CloudFront, 000 also means that the client disconnected before completing the request for that service.


2 Answers

Yes, you can get status code in delegate method -didRecieveResponse:

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;    int code = [httpResponse statusCode]; } 
like image 160
Vladimir Avatar answered Sep 24 '22 17:09

Vladimir


NSHTTPURLResponse* urlResponse = nil; NSError *error = nil; responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 

The aSynchronous request should also have a way to get the NSHTTPURLResponse..

You get the status code like this:

int statusCode = [urlResponse statusCode]; int errorCode = error.code; 

In the case of some much used error codes (like 404) it will get put in the error but with a different code (401 will be -1012).

like image 29
Manuel Avatar answered Sep 22 '22 17:09

Manuel