得到了一个.NET / C#问题......
我需要解析一些“multipart / form-data”格式的输入发布数据,以提取传递的用户名和密码。任何人都知道如何在不编写自己的解析代码的情况下执行此操作吗?
请注意输入的帖子数据如下所示:
---------1075d313df8d4e1d
Content-Disposition: form-data; name="username"
x@y.com
---------1075d313df8d4e1d
Content-Disposition: form-data; name="password"
somepassword
---------1075d313df8d4e1d--
要说明我的代码目前看起来像这样:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Login", BodyStyle = WebMessageBodyStyle.Bare)]
public Stream Login(Stream input)
{
string username = String.Empty;
string password = String.Empty;
StreamReader sr = new StreamReader(input);
string strInput = sr.ReadToEnd();
sr.Dispose();
// Help needed here:
usermame = ?.Parse(strINput, "username");
password = ?.Parse(strINput, "password");
// blah blah blah return login XML response as a Stream
}
答案 0 :(得分:7)
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "Login",
BodyStyle = WebMessageBodyStyle.Bare)]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Required)]
public Stream Login(Stream input)
{
string username = HttpContext.Current.Request.Params["username"];
string password = HttpContext.Current.Request.Params["password"];
}
答案 1 :(得分:2)
您是否可以发布到常规ASP.NET页面(可能是ashx / handler或MVC)并只使用Request.Form
?这支持多部分。