Read status line (status code and reason phrase) using AFNetworking's AFHTTPRequestOperation

拟墨画扇 提交于 2019-12-24 10:56:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!