使用AFNetworking的AFHTTPRequestOperation读取状态行(状态代码和原因短语) [英] Read status line (status code and reason phrase) using AFNetworking's AFHTTPRequestOperation

查看:126
本文介绍了使用AFNetworking的AFHTTPRequestOperation读取状态行(状态代码和原因短语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从iOS应用程序中的 ASIHTTPRequest 切换到 AFNetworking

I am switching from ASIHTTPRequest to AFNetworking in an iOS application.

RF2616(HTTP / 1.1)通过状态代码和原因短语的组合定义了状态行。有时服务器会在此原因短语中添加一些特定的信息,我发现ASIHTTPRequest允许我通过以下方式轻松访问它很方便

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;

我的问题是我找不到使用的任何方法AFHTTPRequestOperation

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".

关于StackOverflow的一些答案表明它应该位于 [operation.response allHeaderFields] 的条目之一中但事实并非如此。

Some answers on StackOverflow suggest that it would be located in one of the entries of [operation.response allHeaderFields] but it's not.

此问题的答案<< 我可以访问原因短语吗?在HTTPHTTP状态行的NSHTTPURLResponse中 >>
建议更改服务器的行为,但这并不总是可用的选项。

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.

有什么想法吗?

推荐答案

NSHTTPURLResponse 没有实现公开数据的方法,并且HTTP RFC不需要这样做。 AFNetworking使用 NSURLRequest / NSURLResponse ,因此AFNetworking也无法将其提供给您。另一方面,ASIHTTPRequest可以使用较低级别的 CFNetwork 框架。

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.

您还可以向Apple提出增强请求,和/或坚持使用ASIHTTPRequest。

You can also file an enhancement request with Apple, and / or stick with ASIHTTPRequest.

这篇关于使用AFNetworking的AFHTTPRequestOperation读取状态行(状态代码和原因短语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆