简体   繁体   中英

How to read post data from Stream object in C#

I used to have a method in mvc application which can read the values from a post request. it was something like this.

[HttpPost]
public ActionResult ResponseFromExternalParty(FormCollection form)

and I was able to read the values as

var authCode = form["auth_code"];

now I need to do the same in another application which is not a mvc application, therefore I cannot use FormCollection object. I was told to use Stream object, so my new method looks like

 public void ResponseFromExternalParty(Stream form)

I cannot get my head around it as how to read post data from this Stream object. if I have to read querystring data I know I could have used something like

HttpUtility.ParseQueryString();

advance thanks for any help.

This answer is only for those guys who will be as confused as I was. Apparently ParseQueryString funtion can also read body of posted data. which is bit confusing as I thought ParseQueryString is only to Parse a query string into a NameValueCollection. So following is how to read posted data from Stream object.

   public void ResponseFromExternalParty(Stream form) {
   var requestBody = new StreamReader(form).ReadToEnd();
   var nameValueCollection = HttpUtility.ParseQueryString(requestBody);

   var authCode = nameValueCollection["auth_code"];
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM