You seem to have the same issue as the question below. You need to check if the HttpMethod is a HEAD request; in that case you can't write bytes to the OutputStream and attempting to do so will throw the exception you are getting.
System.Net.ProtocolViolationException:Bytes to be written to the stream exceed the Content-Length bytes size specified
-
24-06-2022 - |
Question
I write web server code base on HttpListener, but often occur exception like it
"System.Net.ProtocolViolationException:Bytes to be written to the stream exceed the Content-Length bytes size specified".
sample code:
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);
}
}
This exception is not every time request occur. Who can tell me how fix it. Thanks!
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow