FormUrlEncodedContent 长度限制,MS 的任意决定还是 HTTP 规范?

FormUrlEncodedContent's length limitation, MS's arbitary decision or HTTP specification?

我在 new FormUrlEncodedContent(pair); 得到了 "Invalid URI: The Uri string is too long."。经过一些网络搜索,我发现大约有 2000 的长度限制。用户提到 URL 有大小限制,我认为 URL 不应该太长,比如超过 2000 个字符,这是有道理的。但是,就我而言,我并没有尝试将其用作 GET 请求的 URL;我正在尝试将表单数据编码到 POST 请求的正文中。所以,2000年的限制似乎太短了。

我在网上搜索 "x-www-form-urlencoded maximum length",但找不到答案。 the documentation page or the source code 也没有提及长度限制。

由于我正在调用 REST API,因此我无法更改服务器。那么,问题是,"x-www-form-urlencoded" POST 请求的正文长度是否应该像 Microsoft 实施的那样限制在 2000 左右?或者我可以忽略它并放置更长的数据吗?如果是前者,我可以将请求分解成多个请求,但如果是后者,我是否必须按照现有答案显示的那样手动对字符串进行编码,或者是否有这种现有的官方 class任务?

实际限制是(过去是?)65,520 个字符。最近(2019 年 10 月 10 日)在 .NET Core 中有一些关于它的历史 here. Interestingly, it appears this limit was finally removed

在撰写本文时,我不确定 .NET Core 的最新稳定版本中是否存在该更改,但足以说明限制的首要原因非常 "legacy"(不基于任何当前规范),它可能永远不会在 .NET Framework 中删除,并且它已在 .NET Core 中删除。

如果您需要针对 other/older 平台,一种替代方法是使用 Flurl (disclaimer: I'm the author). Its PostUrlEncodedAsync method offers a much cleaner syntax and uses encoding methods designed to work around this limit and other quirks

await "http://example.com".PostUrlEncodedAsync(new { 
    data1 = "value1", 
    data2 = "value2"
});