小编use*_*620的帖子

System.Net.ProtocolViolationException:要写入流的字节超出指定的 Content-Length 字节大小

我基于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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并非每次请求都会出现此异常。谁能告诉我怎么解决。谢谢!

c# outputstream httplistener

3
推荐指数
1
解决办法
3492
查看次数

标签 统计

c# ×1

httplistener ×1

outputstream ×1