C # Why does Request. InputStream read the input stream as null,
When I tested the mobile App interface today, I found that the parameter in the body obtained through the POST method is always empty, but the data can be obtained when the data volume is small, I began to suspect that there is a limit on the length of the POST, and then in the web. the maxRequestLength is modified in config, as shown below:
<system.web>
Then we found that the obtained value is still null. During the debugging process, we found that when the data volume is large, the Position of Request. InputStream is actually at the end Position, as shown in figure
It can be seen that, because Request. InputStream must have been read, Position will be at the end Position. Generally, Positon is 0. Therefore, the parameter in the retrieved body is always blank. After knowing the cause, set the Position to 0 before reading the stream.
var stream = HttpContext.Current.Request.InputStream; stream.Position = 0; using (var streamReader = new StreamReader(stream, Encoding.UTF8)) { requestData = streamReader.ReadToEndAsync().Result; requestData = ("appKey" + appKey + requestData + "timestamp" + timestamp).ToUpper(); stream.Position = 0; }
But why is the Length value of Request. InputStream about 1960 at the end of Position? I don't know why. Hope you can advise me!