获取客户端数据

学习于:http://www.cnblogs.com/fish-li/archive/2011/12/06/2278463.html

有三个比较常见的客户端数据源:QueryString, Form, Cookie
可以在HttpRequest中访问这三大对象,比如,可以从QueryString中获取包含在URL中的一些参数, 可以从Form中获取用户输入的表单数据, 可以从Cookie中获取一些会话状态以及其它的用户个性化参数信息。 除了这三大对象,HttpRequest还提供ServerVariables来让我们获取一些来自于Web服务器变量。

一般情况下,如果我们在事先就能明确知道某个参数是来源于哪个集合,那么直接访问那个集合,问题也就简单了。 然而,更常见的数据来源通常只会是QueryString, Form,而且尤其是当在客户端使用Jquery的$.ajax()这类技术时, 可以很随意地将参数放到QueryString或者是Form中,那么,服务端通常为了也能灵活地应对这一现况, 可以使用Request[]与Request.Params[] 这二种方式来访问这些来自于用户提交的数据。

这二个属性都可以让我们方便地根据一个KEY去【同时搜索】QueryString、Form、Cookies 或 ServerVariables这4个集合。 通常如果请求是用GET方法发出的,那我们一般是访问QueryString去获取用户的数据,如果请求是用POST方法提交的, 我们一般使用Form去访问用户提交的表单数据。而使用Params,Item可以让我们在写代码时不必区分是GET还是POST。

这二个属性唯一不同的是:
A.Item是依次访问这4个集合,找到就返回结果,不会继续访问下去。
B.Params是在访问时,先将4个集合的数据合并到一个新集合(集合不存在时创建), 然后再查找指定的结果。

例子:如果一个URL中含有的参数key与页面提交(submit)的input的name一样的话,用Request["key"]只得到URL中参数的值,用Request.Params["key"]将会得到URL中参数值以及页面中input的值(共两个值)

页面html:

<body>
    <p>
        Item结果:<%= this.ItemValue %></p>
    <p>
        Params结果:<%= this.ParamsValue %></p>
    <form id="form1" action="<%= Request.RawUrl %>" method="post">
    <div>
        <input type="text" name="age" value="123" />
        <input type="submit" value="提交" />
    </div>
    </form>
</body>

后台:

public partial class RequestParamsTest : System.Web.UI.Page
    {
        protected string ItemValue;
        protected string ParamsValue;
        protected void Page_Load(object sender, EventArgs e)
        {
            String[] allKeys = Request.QueryString.AllKeys;
            if (allKeys.Length == 0)
            {
                Response.Redirect("~/RequestParamsTest.aspx?age=45", true);
            }

            ItemValue = Request["age"];
            ParamsValue = Request.Params["age"];
        }
    }

提交之前:Request["age"]的值为45,Request.Params["age"]的值也是45
提交之后  Request["age"]的值为45,Request.Params["age"]的值为【45,123】//把QueryString与Form中的数据合并到一个新的集合中了

小结:尽量不要使用Request.Params

Request[]的实现方式(源代码):

//是一个默认的索引
public string this[string key]
{
    get
    {
        string str = this.QueryString[key];
        if (str != null)
        {
            return str;
        }
        str = this.Form[key];
        if (str != null)
        {
            return str;
        }
        HttpCookie cookie = this.Cookies[key];
        if (cookie != null)
        {
            return cookie.Value;
        }
        str = this.ServerVariables[key];
        if (str != null)
        {
            return str;
        }
        return null;
    }
}

//根据指定的key,依次访问QueryString,Form,Cookies,ServerVariables这4个集合,如果在任意一个集合中找到了,就立即返回。

Request.Params[]实现方式(源代码):

public NameValueCollection Params
{
    get
    {
        if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low))
        {
            return this.GetParams();
        }
        return this.GetParamsWithDemand();
    }
}
private NameValueCollection GetParams()
{
    if( this._params == null ) {
        this._params = new HttpValueCollection(0x40);
        this.FillInParamsCollection();
        this._params.MakeReadOnly();
    }
    return this._params;
}
private void FillInParamsCollection()
{
    this._params.Add(this.QueryString);
    this._params.Add(this.Form);
    this._params.Add(this.Cookies);
    this._params.Add(this.ServerVariables);
}

//先判断_params这个Field成员是否为null,如果是,则创建一个集合,并把QueryString,Form,Cookies,ServerVariables这4个集合的数据全部填充进来, 以后的查询都直接在这个集合中进行。

//Params是NameValueCollection类型,NameValueCollection类在一个键下存储多个字符串值,刚好解释了Request.Params["age"]的值为【45,123】

NameValueCollection类型中,【name】这个key对应一个ArrayList,所以它一个key可以存放多个值,且以逗号隔开

通过调用NameValueCollection类中的GetValues方法,可以得到一个串值,且不含逗号
string[] array = Request.Params.GetValues("name");
if( array != null )
    foreach(string val in array)

其实不仅仅是Request.Params[]存在一个key下保存多个值,只要类型是NameValueCollection的数据源都存在这个问题,QueryString, Form,Param都有这样的问题,只是Param需要合并4种数据源。

原文地址:https://www.cnblogs.com/notebook2011/p/2870471.html