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.
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.
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.
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.
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.
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]; }
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).
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