gpt4 book ai didi

c# - 在 Web API 2 中处理 HEAD 请求时的 Content-Length 0

转载 作者:行者123 更新时间:2023-11-30 23:15:50 27 4
gpt4 key购买 nike

我想在我的 Web API 2 应用程序中处理 HEAD 请求。我从 StrathWeb 的博客文章中复制粘贴了代码 Adding HTTP HEAD support to ASP.NET Web API :

public class HeadHandler : DelegatingHandler
{
private const string Head = "IsHead";

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Head)
{
request.Method = HttpMethod.Get;
request.Properties.Add(Head, true);
}

var response = await base.SendAsync(request, cancellationToken);

object isHead;
response.RequestMessage.Properties.TryGetValue(Head, out isHead);

if (isHead != null && ((bool) isHead))
{
var oldContent = await response.Content.ReadAsByteArrayAsync();
var content = new StringContent(string.Empty);
content.Headers.Clear();

foreach (var header in response.Content.Headers)
{
content.Headers.Add(header.Key, header.Value);
}

content.Headers.ContentLength = oldContent.Length;
response.Content = content;
}

return response;
}
}

当我从 Fiddler 发出 HEAD 请求时:

HEAD http://localhost:54225/api/books/5 HTTP/1.1
User-Agent: Fiddler
Host: localhost:54225

我收到以下回复:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcc2ltb25lXERvY3VtZW50c1xTaW1vbnNEb2N1bWVudHNcSVRcQyNcRGVtb0NvZGVcUkVTVGZ1bFdlYlNlcnZpY2VDbGllbnRERU1PXEJvb2tTZXJ2aWNlXGFwaVxib29rc1w1?=
X-Powered-By: ASP.NET
Date: Sun, 19 Feb 2017 12:09:52 GMT

Content-Length 为 0 但应为 104 字节。

当我向 HeadHandler 添加断点并逐步执行代码时,它似乎在返回响应之前将 Content-Length header 值正确设置为 104 字节。

在 HeadHandler 之后运行的管道中是否有其他步骤,它识别响应没有主体,并将 Content-Length 设置为 0?添加 HeadHandler 之后 WebApiConfig.cs 中唯一的事情是 HttpAttribute 路由的映射和默认路由的设置,在我看来这两者都不太可能导致 Content-Length header 被重置。或者,某处的配置设置是否可能影响响应中返回的 Content-Length?

最佳答案

您需要做的就是设置响应的 Content.Headers 的 Content-Length - 无需为响应创建新内容。无论如何,请求 HEAD 的行为都会从响应中删除正文内容。

这可能是自您引用的文章撰写以来(2013 年)发生的变化,不再需要从头开始创建内容...

这就是你所需要的:

public class HeadHandler : DelegatingHandler
{
private const string Head = "IsHead";

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Head)
{
request.Method = HttpMethod.Get;
request.Properties.Add(Head, true);
}

var response = await base.SendAsync(request, cancellationToken);

object isHead;
response.RequestMessage.Properties.TryGetValue(Head, out isHead);

if (isHead != null && ((bool)isHead))
{
var oldContent = await response.Content.ReadAsByteArrayAsync();

response.Content.Headers.ContentLength = oldContent.Length;
return response;
}

return response;
}
}

关于c# - 在 Web API 2 中处理 HEAD 请求时的 Content-Length 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com