Getting a ProtocolViolationException with Google Chrome and HTTPListener
I have developed an admin tool where I use a simple HTTPListener to return HTML pages. Everything works well with IE and FF but I'm getting a ProtocolVi开发者_开发百科olationException when using Google Chrome.
This is the simplified code (raised from listener.BeginGetContext) that produces the error:
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();
The exception
Bytes to be written to the stream exceed the Content-Length bytes size specified.
is thrown from line
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
What Chrome does or doesn't do to produce this error?
Thanks
I know it's a bit too late, but I've just encountered this problem (not with Chrome) and found this question. This seemingly bullet-proof code fails when you try to write something to OutputStream in response to a HEAD request. ContentLength64 is then set to 0, and for some reason there is no exception thrown when you try to change it, new value is just silently ignored.
精彩评论