Requst对象简介
ASP.NET Request对象功能是从客户端得到数据,常用的三种取得数据的用法是:Request.Form、Request.QueryString,Request。
Request.Form是POST传递方式。 Request.QuerysSring是GET传递方式。 Request则按顺序搜索全部的集合—QueryString、Form、Cookies、ClientCertificate、ServerVarible直到发现第一个匹配值的名称,这样做的集合效率低,并且是不安全的。
QueryString
QueryString集合主要用于收集Http协议中的GET请求发送的数据,如果一个请求事件中,被请求的程序Url中出现“?”后的数据,则表示此次请求方式为Get。
Form
Form集合与QueryString类似,但它用于收集POST方法发送的请求数据(GET方法一般只能传递256字节的数据,而POST可以达到2M)。POST请求必须由Form来发送。
ServerVarible
ServerVarible(环境变量)集合包含了服务器和客户端的系统信息。
POST与GET方式对比
1. GET是从服务器上获取数据,POST是向服务器传送数据。2. GET是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。POST是通过HTTP POST机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。 3. 对于GET方式,服务器端用Request.QueryString获取变量的值,对于POST方式,服务器端用Request.Form获取提交的数据。 4. GET传送的数据量较小,POST传送的数据量较大,一般被默认为不受限制。 5. GET安全性非常低,POST安全性较高。但是执行效率却比POST方法好。
下面来看看相关用法
1.使用QueryString
QueryStirng方法前台代码 1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " ParamPage.aspx.cs " Inherits = " ParamPage " %> 2 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > 3 < html xmlns = " http://www.w3.org/1999/xhtml " > 4 < head runat = " server " > 5 < title ></ title > 6 7 < script type = " text/javascript " > 8 function clickme() { 9 var wind = window.location.href( " ParamPage.aspx?param=newinfo " ); 10 return false ; 11 } 12 </ script > 13 </ head > 14 < body > 15 < form id = " form1 " runat = " server " > 16 < div > 17 < input id = " htmlbutton " type = " button " value = " 传递参数 " onclick = " clickme() " /> 18 </ div > 19 </ form > 20 </ body > 21 </ html >
QueryString后台代码 1 using System; 2 3 public partial class ParamPage : System.Web.UI.Page 4 { 5 protected void Page_Load( object sender, EventArgs e) 6 { 7 if ( ! string .IsNullOrEmpty(Request.QueryString[ " param " ])) 8 { 9 Response.Write(Request.QueryString[ " param " ]); 10 } 11 } 12 }
运行结果:
2.获取Form参数
Form表单前台 1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " ParamPage.aspx.cs " Inherits = " ParamPage " %> 2 3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > 4 < html xmlns = " http://www.w3.org/1999/xhtml " > 5 < head runat = " server " > 6 < title ></ title > 7 </ head > 8 < body > 9 < form id = " form1 " runat = " server " > 10 < div > 11 < asp:TextBox ID = " asp_tb " runat = " server " Text = "" ></ asp:TextBox > 12 < input id = " html_input " type = " text " value = "" name = " html_input " /> 13 < asp:Button ID = " btn_submit " runat = " server " Text = " 提交 " /> 14 </ div > 15 </ form > 16 </ body > 17 </ html > 18
Form表单后台 1 using System; 2 3 public partial class ParamPage : System.Web.UI.Page 4 { 5 protected void Page_Load( object sender, EventArgs e) 6 { 7 if ( ! string .IsNullOrEmpty(Request[ " asp_tb " ])) 8 Response.Write(Request[ " asp_tb " ] + " <br/> " ); 9 if ( ! string .IsNullOrEmpty(Request.Params[ " asp_tb " ])) 10 Response.Write(Request.Params[ " asp_tb " ] + " <br/> " ); 11 if ( ! string .IsNullOrEmpty(Request.Form[ " asp_tb " ])) 12 Response.Write(Request.Form[ " asp_tb " ] + " <br/> " ); 13 14 if ( ! string .IsNullOrEmpty(Request[ " html_input " ])) 15 Response.Write(Request[ " html_input " ] + " <br/> " ); 16 if ( ! string .IsNullOrEmpty(Request.Form[ " html_input " ])) 17 Response.Write(Request.Form[ " html_input " ] + " <br/> " ); 18 if ( ! string .IsNullOrEmpty(Request.Params[ " html_input " ])) 19 Response.Write(Request.Params[ " html_input " ] + " <br/> " ); 20 } 21 } 22
运行结果:
执行:
3.获取ServerVarible及其他
ServerVariables及其他前台 1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " ParamPage.aspx.cs " Inherits = " ParamPage " %> 2 3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > 4 < html xmlns = " http://www.w3.org/1999/xhtml " > 5 < head runat = " server " > 6 < title ></ title > 7 8 < script type = " text/javascript " > 9 function clickme() { 10 var wind = window.location.href( " ParamPage.aspx?param=newinfo " ); 11 } 12 </ script > 13 14 </ head > 15 < body > 16 < form id = " form1 " runat = " server " > 17 < div > 18 < input id = " Button1 " type = " button " value = " Click " onclick = " clickme() " /> 19 </ div > 20 </ form > 21 </ body > 22 </ html > 23
ServerVariables及其他后台 1 using System; 2 3 public partial class ParamPage : System.Web.UI.Page 4 { 5 protected void Page_Load( object sender, EventArgs e) 6 { 7 // 当前浏览器版本IE 7 8 // 客户端 9 Response.Write( " <br>获取客户端浏览器版本号: " + Request.ServerVariables[ " HTTP_USER_AGENT " ]); 10 Response.Write( " <br>获取客户端IP地址: " + Request.ServerVariables[ " REMOTE_ADDR " ]); 11 Response.Write( " <br>获取客户端所使用语言: " + Request.ServerVariables[ " HTTP_ACCEPT_LANGUAGE " ]); 12 Response.Write( " <br>获取请求的方法: " + Request.ServerVariables[ " REQUEST_MOTHOD " ]); 13 Response.Write( " <br>获取请求信息的内容的总符数: " + Request.ServerVariables[ " CONTENT_LENGTH " ]); 14 Response.Write( " <br>获取请求信息类型: " + Request.ServerVariables[ " CONTENT_TYPE " ]); 15 Response.Write( " <br>获取URL的附加信息: " + Request.ServerVariables[ " QUERY_STRING " ]); 16 Response.Write( " <br>获取网关接口: " + Request.ServerVariables[ " GATEWAY_INTERFACE " ]); 17 18 // 服务器 19 Response.Write( " <br>获取服务器IP地址: " + Request.ServerVariables[ " LOCAL_ADDR " ]); 20 Response.Write( " <br>获取服务器的主机名: " + Request.ServerVariables[ " SERVER_NAME " ]); 21 Response.Write( " <br>获取当前执行程序的虚拟目录: " + Request.ServerVariables[ " PATH_INFO " ]); 22 Response.Write( " <br>获取当前执行程序的绝对路径: " + Request.ServerVariables[ " PATH_TRANSLATED " ]); 23 Response.Write( " <br>获取当前程序的文件名(包含虚拟路径): " + Request.ServerVariables[ " SCRIPT_NAME " ]); 24 Response.Write( " <br>获取服务器接受请求的端口: " + Request.ServerVariables[ " SERVER_PORT " ]); 25 Response.Write( " <br>获取服务器遵从的协议及版本号: " + Request.ServerVariables[ " SERVER_PROTOCAL " ]); 26 27 // Browser对象 28 Response.Write( " <br>检查浏览器的类型: " + Request.Browser.Browser); 29 Response.Write( " <br>检查浏览器的版本: " + Request.Browser.Version); 30 Response.Write( " <br>检查浏览器是否支持ActiveX控件: " + Request.Browser.ActiveXControls); 31 Response.Write( " <br>检查浏览器是否支持Cookies: " + Request.Browser.Cookies); 32 Response.Write( " <br>检查浏览器是否支持VBScript: " + Request.Browser.VBScript); 33 34 // 其他 35 Response.Write( " <br>客户端浏览器的版本: " + Request.UserAgent); 36 Response.Write( " <br>当前网页虚拟路径: " + Request.RawUrl); 37 Response.Write( " <br>当前网页虚拟路径: " + Request.ServerVariables[ " url " ]); 38 Response.Write( " <br>实际目录: " + Request.PhysicalApplicationPath); 39 Response.Write( " <br>实际地址: " + Request.PhysicalPath); 40 Response.Write( " <br>实际地址: " + Request.ServerVariables[ " path_translated " ]); 41 Response.Write( " <br>服务器名: " + Request.ServerVariables[ " server_name " ]); 42 Response.Write( " <br>服务器IP: " + Request.UserHostName); 43 Response.Write( " <br>服务器IP: " + Request.UserHostAddress); 44 Response.Write( " <br>服务器传送方式: " + Request.RequestType); 45 Response.Write( " <br>当前浏览器是否支持背景音乐: " + Request.Browser.BackgroundSounds); 46 Response.Write( " <br>浏览器是否支持框架 " + Request.Browser.Frames); 47 Response.Write( " <br>当前使用的操作系统 " + Request.Browser.Platform); 48 } 49 } 50
运行结果:
posted on 2010-07-06 17:26 Johnny_Z 阅读( ...) 评论( ...) 编辑 收藏转载于:https://www.cnblogs.com/Johnny_Z/archive/2010/07/06/1772301.html
相关资源:数据结构—成绩单生成器