我可以进入“原因短语"页面吗?从NSHTTPURLResponse中的HTTP状态行 [英] Can I access "Reason Phrase" from the HTTP Status-Line in NSHTTPURLResponse

查看:95
本文介绍了我可以进入“原因短语"页面吗?从NSHTTPURLResponse中的HTTP状态行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指定HTTP的RFC 2616说-在第6.1.1节中-状态行的一部分是一个3位数的数字状态代码和一个文本原因短语".

RFC 2616 specifying HTTP says - in section 6.1.1 - that part of the Status-Line is a 3-digit numeric Status Code AND a textual "Reason Phrase".

我正在构建一个iPhone应用程序,该应用程序使用NSURLConnection通过HTTP访问数据.我可以毫无问题地获取HTTP状态代码,但是如何访问原因短语"?

I am building an iPhone app, that is using NSURLConnection to access data over HTTP. I can get the HTTP Status Code without problems, but how can I access the "Reason Phrase"?

这是我的连接:didReceiveResponse:方法

Here's my connection:didReceiveResponse: method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    httpStatusCode = [httpResponse statusCode];
    // Reason Phrase ??
}

具体来说,我不是要表示代码xxx的含义的解释"文本.我可以在RFC中查找这些内容,这些内容是静态的.我的意思是服务器在状态行中生成的文本.这样的状态行的示例为:

To be specific, I do NOT mean the "explanation of what a code xxx means" text. I can lookup that in the RFC and those are static. I mean the text the server produced in the status line. An example of such a status line would be:

HTTP/1.1 412 ClientAppVersion: 0.10 < 0.11

,原因短语在此处为"ClientAppVersion:0.10< 0.11".

and the Reason Phrase would be "ClientAppVersion: 0.10 < 0.11" here.

此示例还提示了我要执行的操作.我正在构建类似REST的API,因此,我应该使用HTTP状态代码来指示错误.但是HTTP状态代码是为HTTP而不是为我的应用程序发明的,因此我尝试将额外的信息填充到原因短语"中.

This example also gives a hint at what I am trying to do. I am building a REST-like API, and as such, I should use the HTTP status codes to indicate errors. But HTTP status codes were invented for HTTP and not for my app, so I try to cram extra information into the Reason Phrase.

推荐答案

关注先前的评论(这并不是您要找的答案).

Following up on previous comments (this is not quite the answer you're looking for).

HTTP规范(RFC 2616)声明关于状态码和原因短语:

The HTTP specification (RFC 2616) states, about status codes and reason-phrase:

状态代码供以下人员使用 自动机,原因短语为 适用于人类用户.这 不需要客户检查或 显示原因短语.

The Status-Code is intended for use by automata and the Reason-Phrase is intended for the human user. The client is not required to examine or display the Reason- Phrase.

从文字上很明显,不应期望HTTP客户端读取原因短语.实际上,如果可能的话,它通常是本地化的版本(不一定是服务器发送的版本).

It's quite clear, from the text, that an HTTP client shouldn't be expected to read the reason-phrase. In fact, it's often a localized version that may be presented, if at all (not necessarily the one sent by the server).

拥有诸如HTTP之类的标准和规范的目的是能够期望不同的兼容实现(例如您的服务器和iOS库)能够互操作.如果您弯腰规格,应该会遇到问题.特别是,如果您要使用的库没有使您能够访问原因短语,请不要感到惊讶.

The purpose of having standards and specifications such as HTTP is to be able to expect different compliant implementations (such as your server and the iOS libraries) to be able to interoperate. You should expect problems if you bend the specifications. In particular, don't be surprised if the library you want to use doesn't give you access to the reason-phrase.

我不太确定如何解释您的评论(我正在弯曲HTTP以使其符合REST的想法.")我可以向您保证,可以使用HTTP来实现REST,而无需进行这种弯曲.我不确定您从何处弯曲HTTP以适合REST想法...

I'm not quite sure how to interpret your comment ("I am bending HTTP to make it fit the REST idea.") I can assure you that REST can be implemented using HTTP without this sort of bending. I'm not sure where you got this idea of bending HTTP to fit the REST idea...

如果您要实现某种方法来以REST方式为错误提供原因,则应在响应消息正文中(甚至可能在自定义标头中)给出原因,而不是在原因短语中给出原因.即使是纯文本响应,它也比原因短语要好.例如:

If you want to implement something to give an error reason the REST way, the cause should be given in the response message-body (or even maybe in a custom header), not in the reason-phrase. Even if it's a plain-text response, it's better than the reason phrase. For example:

代替:

HTTP/1.1 412 ClientAppVersion: 0.10 < 0.11

使用:

HTTP/1.1 412 Precondition Failed
Content-Type: text/plain

ClientAppVersion: 0.10 < 0.11

或者也许:

HTTP/1.1 412 Precondition Failed
Content-Type: text/plain
X-My-Error: ClientAppVersion: 0.10 < 0.11

请注意,无论如何您都应该返回一个消息正文(除非204). 状态代码412 也与基于前提的条件非常相关.在标题(您可能正在使用)上:

Note that you should return a message-body anyway (unless 204). Status code 412 is also quite specifically related to preconditions based on headers (which you may be using):

一个或多个给出的前提 评估的请求标头字段 在测试时为假 服务器.此响应代码允许 客户对 当前资源元信息 (标题字段数据),从而防止 所请求的方法是 应用于除 一个打算.

The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server. This response code allows the client to place preconditions on the current resource metainformation (header field data) and thus prevent the requested method from being applied to a resource other than the one intended.

这篇关于我可以进入“原因短语"页面吗?从NSHTTPURLResponse中的HTTP状态行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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