簡體   English   中英

System.Net.ProtocolViolationException:要寫入流的字節超過了指定的Content-Length字節大小

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

我基於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);
    }
}

並非每次請求都發生此異常。 誰能告訴我如何解決。 謝謝!

您似乎與以下問題有相同的問題。 您需要檢查HttpMethod是否為HEAD請求; 在這種情況下,您無法將字節寫入OutputStream,並且嘗試這樣做將引發您所獲取的異常。

Chrome的ProtocolViolationException

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM