The first request fails with an HTTP 400 (Bad Request) error after reading HttpRequest.InputStream

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();
+3
source share
3 answers

Could not find a way to read the contents of the request during Application_Start without disrupting the internal operation of ASP.NET/IIS. Instead, it turned out that this would not happen until Application_Startit ended (and also did not happen from the moment it was launched Application_End, which also turned out to be problematic and created access violations).

+1
source

You should not use the use block because it has the effect to close the reader and, as a result, close the httprequest input stream

+1
source

Request.InputStream Application_Start - . Request Application_Start " ".

, , , Application_BeginRequest - .

:

  • Application_Start
    , . , , . , .

  • Application_BeginRequest
    - , , ..

See these SO articles for more information:

0
source

Source: https://habr.com/ru/post/1771157/


All Articles