问题
I am switching from ASIHTTPRequest
to AFNetworking
in an iOS application.
RF2616 (HTTP/1.1) defines a "status-line" by the combination of "Status Code" and "Reason Phrase". Sometimes the server adds some specific information in this "Reason Phrase" and I found it rather handy that ASIHTTPRequest allowed me to access it easily via
ASIHTTPRequest *request = ...;
NSString *reason = request.responseStatusMessage;
My problem is that I can't find any way to do it with AFHTTPRequestOperation
NSMutableURLRequest *request = ...;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
In both completion/failure block of the operation i can get the status code by doing:
int statusCode = [operation.response statusCode];
But I can't find where to get the "Reason Phrase".
Some answers on StackOverflow suggest that it would be located in one of the entries of [operation.response allHeaderFields]
but it's not.
An answer to this question << Can I access "Reason Phrase" from the HTTP Status-Line in NSHTTPURLResponse >> suggests changing how the server behaves but that's not always an available option.
Any idea?
回答1:
NSHTTPURLResponse
does not implement a method that exposes the data, and the HTTP RFC does not a require it to. AFNetworking uses NSURLRequest
/ NSURLResponse
, and so AFNetworking cannot give it to you either. ASIHTTPRequest can, on the other hand, because it uses the lower-level CFNetwork
framework.
The advice given in the other answers, to move this information to your headers or body, is the best answer.
You can also file an enhancement request with Apple, and / or stick with ASIHTTPRequest.
来源:https://stackoverflow.com/questions/18887429/read-status-line-status-code-and-reason-phrase-using-afnetworkings-afhttprequ