[英]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. 我以前在mvc应用程序中有一个方法可以从发布请求中读取值。 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. 现在,我需要在不是mvc应用程序的另一个应用程序中执行相同的操作,因此无法使用FormCollection对象。 I was told to use Stream object, so my new method looks like
有人告诉我使用Stream对象,所以我的新方法看起来像
public void ResponseFromExternalParty(Stream form)
I cannot get my head around it as how to read post data from this Stream object. 我无法理解如何从此Stream对象读取发布数据。 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.
显然ParseQueryString函数也可以读取已发布数据的主体。 which is bit confusing as I thought ParseQueryString is only to Parse a query string into a NameValueCollection.
这有点令人困惑,因为我认为ParseQueryString仅是将查询字符串解析为NameValueCollection。 So following is how to read posted data from Stream object.
因此,以下是如何从Stream对象读取已发布数据的方法。
public void ResponseFromExternalParty(Stream form) {
var requestBody = new StreamReader(form).ReadToEnd();
var nameValueCollection = HttpUtility.ParseQueryString(requestBody);
var authCode = nameValueCollection["auth_code"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.