c# - IIS 和 RestSharp -> 响应主体因 40x 错误而中断?

标签 c# asp.net-mvc-3 iis iis-7 restsharp

情况

看起来很简单:我有一个在 IIS 7 上运行的服务。一个客户端 POSTS 数据(应用程序/json),我在接受数据之前验证数据。

如果我不接受它,我真的很想返回 406 和与主体相同的数据(可能已更改/更正)[1]。不幸的是,这会导致响应主体被截断,也就是无效的 json。

首先,让我们为我的错误启用直通,因为 IIS 试图以其他方式变得聪明:

<httpErrors errorMode="Detailed" existingResponse="PassThrough"><br/> </httpErrors>

我代码的相关部分在道德上与此等效:

HttpResponseBase response = context.HttpContext.Response;

response.StatusCode = StatusCode;
response.StatusDescription = StatusDescription;

if (!string.IsNullOrEmpty(ContentType))
    response.ContentType = ContentType;
else
    response.ContentType = "application/json";

if (ContentEncoding != null)
    response.ContentEncoding = ContentEncoding;

using (var sw = new StreamWriter(response.OutputStream))
{
    sw.Write(JsonConvert.SerializeObject(Data));
}

在客户端,我目前正在做一个幼稚的(寻找json截断的问题)

var response = myRestClient.Execute(myRestRequest);

问题

如果响应返回状态代码 200,我会得到这个

response.ContentLength == 69345
response.RawBytes.Length == 69345

如果我只更改返回的状态代码(在我的情况下为 406)什么都不,返回完全相同的数据,我会看到:

response.ContentLength == 69345
response.RawBytes.Length == 65536 // <--- Not! Good!

现在,65536 是一个太神奇的数字,不可能是巧合或宇宙射线的可重复结果。谁在现在试图变聪明并在我的数据超过无符号短整型长度时丢弃我的数据?我现在将尝试深入研究 RestSharp 代码库,但我真的很怀疑 IIS 又在耍我..

1:如果这是个坏主意,请详细说明原因?

最佳答案

看看这个帖子:RestSharp RestResponse is truncating content to 64 kb

发生这种情况是因为 RestSharp 使用 .NET Framework 中的 HttpWebRequest 类。此类具有名为 DefaultMaximumErrorResponseLength 的静态属性。该属性决定了错误响应的最大长度,该属性的默认值为 64Kb。

关于c# - IIS 和 RestSharp -> 响应主体因 40x 错误而中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10519948/

相关文章:

c# - Win Mobile 6.5.3 模拟器无法连接到互联网

c# - ASP.NET MVC 中 TextBoxFor 的默认值

asp.net - 为什么在调用 Application_Error() 时 Response.StatusCode 设置为 200?

c# - ConnectionString 属性尚未初始化 IIS 8.5

c# - 无法访问已实现的属性(从接口(interface))

java - Java 或 C# 中是否存在命名空间污染(如在 C++ 中)?

c# - C# 中的 eBay API GetSellerList 调用返回错误 14005 'Web Service framework internal error. execute exception.'

c# - 如何捕获mvc-3中的url进行重定向?

asp.net - .resx 文件更改时应用程序域重新启动。有什么办法可以避免这种情况?

asp.net-core - 在 Windows Server 上调用 ASP.NET Core 2.1 Web API 时出现 "500 - Internal server error"