Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ProtocolViolationException 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

like image 451
patrice Avatar asked Jun 03 '11 13:06

patrice


1 Answers

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.

like image 124
user1096188 Avatar answered Sep 24 '22 16:09

user1096188