我基于 HttpListener 编写 Web 服务器代码,但经常出现类似的异常
“System.Net.ProtocolViolationException:要写入流的字节超过指定的 Content-Length 字节大小”。
示例代码:
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);
}
}
并非每次请求都发生此异常。谁能告诉我怎么修。谢谢!