I am developing an asmx web service (i.e. ASP.NET 2.0).
Here is a piece of code that can read the contents of an HTTP request (via HttpContext.Current.Request.InputStream) when processing it. I understand that an InputStream can only be read once for a request, and I'm sure I never try to read it more than once.
The problem is that if the InputStream is read in the early stages of the application life cycle (for example, after pskill w3wp, during Application_Start), the HTTP request will fail with HTTP error 400 - Bad Request is given without explanation, no exception is thrown and an entry in the httperr log missing. If it is read later (for example, inside the web method itself), the requests are executed normally, regardless of whether the InputStream is read or not. Application_Start works fine if the InputStream is not readable.
Is this some kind of ASP.NET error? IIS error? Or am I doing something wrong, daring to read an InputStream? And if so, is there another way to look at the raw content of a request without violating the internal actions of IIS / ASP.NET?
In short, adding this code to Application_Start is enough to reproduce this error:
using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream))
reader.ReadToEnd();
source
share