Reputation: 2647
I am trying to pull some data from a server (I don't own the server) however it returns some needed data with a http 500 error code, however NSString stringWithContentsOfURL returns nil if the server returns http code 500.
I'd like to retrieve the data even if the server returns 500.
Upvotes: 2
Views: 940
Reputation: 18805
You can carefully use this code (never from GUI thread):
NSURLRequest * request = ...
NSError * error = nil;
NSURLResponse * response = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
NSInteger httpCode = [(NSHTTPURLResponse *)response statusCode];
Upvotes: 1
Reputation: 98002
NSURLConnection (as part of the URL Loading System) will give you the lower-level control over the request and the interpretation of the response; you will have to implement a few call-back methods to handle fine grained control of the request (and you can opt to invoke it synchronously to mimic stringWithContentsOfURL:
, but it will give you what you want.
[NSString stringWithContentsOfURL:]
is a great convenience, but in your case you want to circumvent the way cocoa interprets HTTP response codes, so that requires a smidge more work.
Upvotes: 3