Recently I needed to post some json data from browser to web server. I used the following codes to read the input stream:
System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string json = sr.ReadToEnd();
There's one problem with the above codes, that is the json variable will hold empty string even though I am sure that data is posted to the web server. The resolution of the problem is to always set the stream position to 0. The resulting codes looks like this:
Request.InputStream.Position = 0; System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
string json = sr.ReadToEnd();
Now with the first line of the codes, you will be able to get the stream data.
3 comments:
This is exactly what I needed, thanks!
Thank you! That solved my problem as well!
You made my day :)
Post a Comment