如何使用 C# 将 POST 数据作为数组获取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1824601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get POST data as array with C#
提问by Kahn
I'm developing an ASP.NET handler with C# that receives an array of data posted from an external Flash program. How can I use the data as array? It seems the HTTP Context used in ASP.NET automatically html-decodes the post data so that it comes out as a simple string. The separator in this case is comma (could be it's always a comma, never worked with this before).
我正在使用 C# 开发一个 ASP.NET 处理程序,它接收从外部 Flash 程序发布的数据数组。如何将数据用作数组?似乎在 ASP.NET 中使用的 HTTP 上下文会自动对发布数据进行 html 解码,以便它作为一个简单的字符串出现。在这种情况下,分隔符是逗号(可能总是逗号,以前从未使用过)。
Seems I need either of two things:
似乎我需要两件事之一:
1) A way to get to the data in its html-encoded form, where the comma used as a separator for the arrays can ONLY represent real arrays instead of customer form input (the customer input comma would remain encoded at this point).
1) 一种以 html 编码形式获取数据的方法,其中用作数组分隔符的逗号只能表示真实数组而不是客户表单输入(此时客户输入逗号将保持编码)。
2) A way to simulate the PHP print_r()
, var_dump()
function (don't know PHP myself, but I'm told that does the trick there) to dump the variable into an array.
2)一种模拟 PHP print_r()
,var_dump()
函数(我自己不了解 PHP,但有人告诉我这样做的技巧)将变量转储到数组中的方法。
So any help on how to do either would be appreciated. Thanks in advance!
因此,任何有关如何做的任何帮助都将不胜感激。提前致谢!
Edit 1:The data being posted can be for example a bunch of addresses, postalcodes and optional extra info. Such as address=testroad%5F5,another%5Ftestroad%5F6&postalcode=12345,56789&extrainfo=,firstwasempty
. In that example, there were two addresses (%5F
equals whitespace), two postalcodes, but only the second address contained extrainfo
as the space before the comma was empty. Now once more, as English isn't my mothertongue, the problem was that possible customer-written comma gets mixed with the actual array-splitting comma after decoding.
编辑 1:发布的数据可以是例如一堆地址、邮政编码和可选的额外信息。比如address=testroad%5F5,another%5Ftestroad%5F6&postalcode=12345,56789&extrainfo=,firstwasempty
。在该示例中,有两个地址(%5F
等于空格)、两个邮政编码,但只有第二个地址extrainfo
作为逗号前的空格为空。现在再一次,由于英语不是我的母语,问题是可能的客户编写的逗号在解码后与实际的数组拆分逗号混合在一起。
采纳答案by Darin Dimitrov
public void ProcessRequest(HttpContext context)
{
using (var reader = new StreamReader(context.Request.InputStream))
{
string postedData = reader.ReadToEnd();
foreach (var item in postedData.Split(new [] { '&' }, StringSplitOptions.RemoveEmptyEntries))
{
var tokens = item.Split(new [] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length < 2)
{
continue;
}
var paramName = tokens[0];
var paramValue = tokens[1];
var values = paramValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var value in values)
{
var decodedValue = context.Server.UrlDecode(value);
// Do something with the decoded value which corresponds to paramName
}
}
}
}
回答by deerchao
Check HttpRequest.InputStream property: http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx
检查 HttpRequest.InputStream 属性:http: //msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx
回答by David d C e Freitas
I found
我发现
Request.Params[null]
refers to the RAW data posted to the page in C# ASP.NET.
是指在 C# ASP.NET 中发布到页面的 RAW 数据。
回答by Jota Santos
I had the same trouble and found this post, but just now I've found a better way to do it.
我遇到了同样的麻烦并找到了这篇文章,但刚才我找到了一个更好的方法。
You should put the same name on all the POST variables, so the data to be posted is:
您应该在所有 POST 变量上放置相同的名称,因此要发布的数据是:
address=AddressValue1&address=AddressValue2&address=AddressValue3
地址=地址值1&地址=地址值2&地址=地址值3
In the C# code script you can then access the data parameters with:
在 C# 代码脚本中,您可以使用以下命令访问数据参数:
string[] addresses = this.Request.Form.GetValues("address");
foreach (string address in addresses)
{
// Your code here ...
}