解析"的multipart / form-data的"在.NET / C# [英] Parsing "multipart/form-data" in .NET/C#

查看:364
本文介绍了解析"的multipart / form-data的"在.NET / C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个.NET / C#的问题...

我需要解析一些输入数据后,多数民众赞成在一个多部分/表单数据的格式来提取传递的用户名和密码。任何人都知道如何做到这一点,而无需编写自己的分析code?

请注意,输入数据后看起来是这样的:

  --------- 1075d313df8d4e1d
内容处置:表格数据; NAME =用户名

x@y.com
--------- 1075d313df8d4e1d
内容处置:表格数据; NAME =密码

somepassword
--------- 1075d313df8d4e1d--
 

要demostrate我的code看起来像这样的时刻:

  [OperationContract的]
[WebInvoke(方法=POST,UriTemplate =登录,BodyStyle = WebMessageBodyStyle.Bare)
公共流登录(流输入)
{
    字符串的用户名=的String.Empty;
    串密码=的String.Empty;

    StreamReader的SR =新的StreamReader(输入);
    字符串strInput = sr.ReadToEnd();
    sr.Dispose();

    //帮助需要在这里:
    usermame = .Parse(strINput,用户名)?;
    密码= .Parse(strINput,密码)?;

    //等等等等等等返回登录XML响应为流
}
 

解决方案

Marc的得到它。使用ASP.NET兼容性要求模式最简单的方法是应用<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.aspnetcompatibilityrequirementsmode.aspx">AspNetCompatibilityRequirementsMode属性对您的操作。然后,你有机会获得<一href="http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.params%28VS.71%29.aspx">HttpContext形成PARAMS 。这里是你如何去了解它:

  [OperationContract的]
        [WebInvoke(方法=POST
                    UriTemplate =登陆,
                    BodyStyle = WebMessageBodyStyle.Bare)
        [AspNetCompatibilityRequirements(RequirementsMode =
                    AspNetCompatibilityRequirementsMode.Required)]
        公共流登录(流输入)
        {
            字符串的用户名= HttpContext.Current.Request.Params [用户名];
            串密码= HttpContext.Current.Request.Params [密码];
        }
 

Got a .NET/C# question...

I need to parse some input post data thats in a "multipart/form-data" format to extract the passed username and password. Anyone know how to do this without writing my own parsing code?

Note the input post data looks something like this:

---------1075d313df8d4e1d
Content-Disposition: form-data; name="username"

x@y.com
---------1075d313df8d4e1d
Content-Disposition: form-data; name="password"

somepassword
---------1075d313df8d4e1d--

To demostrate my code looks something like this at the moment:

[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
}

解决方案

Marc's got it. The easiest way to use the ASP.NET compatibility requirements mode is to apply the AspNetCompatibilityRequirementsMode attribute on your operation. Then you have access to the HttpContext form params. Here's how you'd go about it:

        [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"];
        }

这篇关于解析&QUOT;的multipart / form-data的&QUOT;在.NET / C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆