WebException: "The message length limit was exceeded"

An uncommon WebException thrown by HttpWebRequest.GetResponse() has the message “The underlying connection was closed: The message length limit was exceeded.”

By default, HttpWebResponse only allows 64KB of HTTP headers; any more and it will throw this exception.

To resolve the issue, set the MaximumResponseHeadersLength property on the request to a larger value, or to -1 for no limit. (Note that the value is expressed in kilobytes, so the default value 64 allows 65,536 bytes of headers.)

Posted by Bradley Grainger on January 04, 2012