C # Why does Request. InputStream read the input stream as null,

Source: Internet
Author: User

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!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.