gpt4 book ai didi

c# - 使用 Google Chrome 和 HTTPListener 获取 ProtocolViolationException

转载 作者:太空狗 更新时间:2023-10-29 23:18:11 24 4
gpt4 key购买 nike

我开发了一个管理工具,我在其中使用一个简单的 HTTPListener 来返回 HTML 页面。在 IEFF 上一切正常,但在使用 Google Chrome 时出现ProtocolViolationException

这是产生错误的简化代码(从 listener.BeginGetContext 引发):

byte[] buffer = System.Text.Encoding.UTF8.GetBytes("<html><body>response sent!</body></html>");
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length); //<-- crashes here
context.Response.OutputStream.Close();
context.Response.Close();

异常

要写入流的字节超过指定的 Content-Length 字节大小。

从线上抛出

context.Response.OutputStream.Write(buffer, 0, buffer.Length);

Chrome 做了什么或没有做什么来产生这个错误?

谢谢

最佳答案

我知道有点晚了,但我刚刚遇到这个问题(不是 Chrome)并发现了这个问题。当您尝试向 OutputStream 写入内容以响应 HEAD 请求时,这段看似万无一失的代码会失败。然后将 ContentLength64 设置为 0,并且出于某种原因,当您尝试更改它时不会抛出异常,新值将被静默忽略。

关于c# - 使用 Google Chrome 和 HTTPListener 获取 ProtocolViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6227534/

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