长期饭票

大家好,请喊我序员!
QQ:15838986
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Request,Request.QueryString,Request.Form

Posted on 2012-04-10 13:27  Baode  阅读(441)  评论(0编辑  收藏  举报

Request.QueryString:获取 HTTP 查询字符串变量集合。一般是获取Get方式提交的数据,包含由客户端发送的查询字符串变量的集合。

例如,如果请求URL 为 http://www.cnblogs.com/errorif/posts.aspx?id=44,则 System.Web.HttpRequest.QueryString 的值为“id=44”。

 

Request.Form:获取窗体变量集合。一般是获取以Form形式Post提交的数据

 

Request:从几个集合取数据是有顺序的,从前到后的顺序依次是 QueryString,Form,Cookie最后是ServerVariables。Request对象按照这样的顺序依次搜索这几个集合中的变量,如果有符合的就中止。

其实现代码为:

 1 public string this[string key]
 2 {
 3     get
 4     {
 5         string str = this.QueryString[key];
 6         if (str != null)
 7         {
 8             return str;
 9         }
10         str = this.Form[key];
11         if (str != null)
12         {
13             return str;
14         }
15         HttpCookie cookie = this.Cookies[key];
16         if (cookie != null)
17         {
18             return cookie.Value;
19         }
20         str = this.ServerVariables[key];
21         if (str != null)
22         {
23             return str;
24         }
25         return null;
26     }
27 }
28  

 

我们来分析下.
假设有个页面 test.aspx?id=111
这里我们的页面是用GET的方法.这时用Request.QueryString("id")与Request("id")是一样得,应该如果不指定REQUEST得集合,首先就会从QueryString搜索.

而如果我们的页面是用的是POST的方法发送数据给test.asp,那么用Request.QueryString("id")是不行的了(他只能取GET),而要用Request.From("id"),而如果还用Request("id")他也能取到数据,但先检测QuerySstring的值,显然速度就慢了.

 

 

下面是个检测的例子:

 

<% 
If Request("submit")<>"" then 
    Response.Write 
"直接取:"& Request("username"& "<br>" 
    Response.Write 
"取Get:" & Request.QueryString("username"& "<br>" 
    Response.Write 
"取Post:" & Request.Form("username"& "<br>" 
End if 
%> 
<form name=form1 action="" method=post> 
<input type=test name="username" value="postuser"> 
<input type=submit name="submit" value="test"> 
</form>