gpt4 book ai didi

c# - System.Net.ProtocolViolationException :Bytes to be written to the stream exceed the Content-Length bytes size specified

转载 作者:行者123 更新时间:2023-11-30 22:15:01 32 4
gpt4 key购买 nike

我用HttpListener写web server代码,但是经常出现这样的异常

"System.Net.ProtocolViolationException:Bytes to be written to the stream exceed the Content-Length bytes size specified".

示例代码:

context.Response.Output = System.Text.Encoding.UTF8.GetBytes("xxx");
if (context.Response.Output != null && context.Response.Output.Length > 0)
{
context.Response.ContentLength64 = context.Response.Output.Length;
using (var stream = context.Response.OutputStream)
{
stream.Write(context.Response.Output, 0, context.Response.Output.Length);
}
}

这种异常不是每次请求都会发生。谁能告诉我怎么解决。谢谢!

最佳答案

您似乎遇到了与以下问题相同的问题。您需要检查 HttpMethod 是否为 HEAD 请求;在这种情况下,您无法将字节写入 OutputStream 并且尝试这样做会引发您遇到的异常。

ProtocolViolationException with Chrome

关于c# - System.Net.ProtocolViolationException :Bytes to be written to the stream exceed the Content-Length bytes size specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18203552/

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