C#,HTTPClient-IfMatch格式要求? [英] C#, HTTPClient - IfMatch format requirements?

查看:111
本文介绍了C#,HTTPClient-IfMatch格式要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将.Net 4.5中的 System.Net.Http.HttpClient 与CouchDb一起使用。

I am using the System.Net.Http.HttpClient found in .Net 4.5 in conjunction with CouchDb.

我有一个文档的版本是: 3-789d4d2b33bf4505f8f23fd4a1025a4e

问题是我无法通过请求中的 If-Match 标头标志来使用它。

The issue is that I can't get that to work with the If-Match header flag on the request.

var req = new HttpRequestMessage(HttpMethod.Delete, url);
req.Headers.IfMatch.Add(
    new EntityTagHeaderValue("3-789d4d2b33bf4505f8f23fd4a1025a4e"));

EntityTagHeaderValue 导致格式异常:


值'3-789d4d2b33bf4505f8f23fd4a1025a4e'的格式无效。

"The format of value '3-789d4d2b33bf4505f8f23fd4a1025a4e' is invalid."

我试图通过以下方式使用纯字符串添加它: req.Headers.Add(string,string),仍然没有运气。

I have tried to add it using pure strings via: req.Headers.Add(string, string), still no luck.

我找到的有效解决方案是:

The working solution I have found is:

req.Headers.TryAddWithoutValidation("If-Match", rev);

问题是, If-Match的要求是什么标头?

推荐答案

标记必须是实体标记第3.11节中定义的code> / a>,然后将其定义为引号字符串(请参见 HTTP规范的第2.2节),还可以选择在 W / 之前加注以表示弱标签。粗略地说,这意味着它几乎可以是任何文本,但必须用双引号引起来。 (如果存在的话, W / 前缀在开头的引号之前。)引号内的文本可以是除控制字符或之外的任何内容。(还有 如果在 \ 之前出现,则可以出现。

The tag is required to be an entity-tag as defined in section 3.11 of the HTTP specification, which in turn defines it as a quoted-string (see section 2.2 of the HTTP specification), optionally preceded by W/ to indicate a 'weak' tag. Roughly speaking, this means it can be pretty much any text at all but it has to be enclosed in double quotes. (If present, the W/ prefix comes before the opening quote.) The text within quotes can be anything except control characters or ". (And a " is allowed to appear if preceded by a \).

如果HTTP服务器报告ETag为 3-789d4d2b33bf4505f8f23fd4a1025a4e ,然后违反了规范。确实应该类似于 3-789d4d2b33bf4505f8f23fd4a1025a4e

If an HTTP server reports an ETag as 3-789d4d2b33bf4505f8f23fd4a1025a4e then it's violating the spec. It should really be something like "3-789d4d2b33bf4505f8f23fd4a1025a4e".

实体标签缺少引号。显然, HttpClient 在执行规则时有点不寻常,如果您必须处理违反规则的服务器,则它会显得有些麻烦。

That said, it seems to be quite common for entity tags to be missing the quotes. Apparently HttpClient is slightly unusual in enforcing the rules, making it slightly cumbersome if you're having to deal with a server that breaks the rules.

请注意,规范中存在错误。已发布的HTTP 1.1规范允许带引号的字符串以反斜杠结尾,例如 foo\ 或什至 \ 。但是,这不是本意。这样做的目的是,反斜杠将始终跟在另一个字符之后,并以在结束引号之前不加 \ 来区分,这是后两个示例所违反的。请参见此错误报告。它已在下一版HTTP 1.1规范(HTTP bis,它似乎是Web标准的方式,将于现在的任何十年发布)的当前草案中得到修复。因此,尽管今天这两个字符串在技术上是合法的,但实现者最好避免这种情况。

Note that there's a bug in the spec. The published HTTP 1.1 spec allows quoted strings to end in a backslash, e.g., "foo\" or even just "\". However, this was not the intention. The intention was that a backslash would always be followed by another character, and that the closing quote would be distinguished by not being preceded by a \, something those last two examples violate. See this bug report. It has been fixed in the current draft of the next version of the HTTP 1.1 spec (HTTP bis, which, as seems to be the way with web standards, is due to be published any decade now). So although those two strings are technically legal today, implementors would be wise to avoid that sort of thing.

这篇关于C#,HTTPClient-IfMatch格式要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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