ASP.NET 以 Request.Querystring、Request.Form 或 Request.Params 获取客户端的数据
2012-07-11 10:36
816 查看
本文内容
本文旨在说明客户端向服务器如何发送数据,以及服务器如何接收。虽然这种实现方式现在已经看不到了,但是这种机制是不变的,已经变成了一种底层实现,所以还是有必要了解的。ASP.NETGet与Post方式
ASP.NETRequest.querystring、Request.Form和Request.Params比较
示例
ASP.NET原始请求(HTTPGet)与回发(HTTPPost)方式
原始请求(HTTPGet)方式是从服务器上获取数据,主要用于查询;回发(HTTPPost)方式是向服务器传送数据,主要用于增删改。原始请求(HTTPGet)方式,以键值形式,把向服务器发送的数据添加到表单form的action属性的URL中。回发(HTTPPost)将表单内各个键和其值放到HTMLheader中,发送到form的action属性所指的URL,对用户隐藏。
原始请求(HTTPGet)方式,服务器端通过Request.QueryString获取数据。而回发(HTTPPost)方式,服务器端通过Request.Form来获取。
原始请求(HTTPGet)方式传送的数据量较小,不能大于2KB。回发(HTTPPost)方式传送的数据量较大,默不受限制。
原始请求(HTTPGet)方式安全性很低。回发(HTTPPost)方式较高。原始请求(HTTPGet)方式的安全性较回发(HTTPPost)方式要差些,包含机密信息的话,建议用回发(HTTPPost)数据提交方式。
原始请求(HTTPGet)方式执行效率比回发(HTTPPost)方式好。
若不指定的form的method和action属性,则ASP.NET默认为Post方式,则数据回发给自己。
示例:
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
<scriptrunat="server">1:
2:protectedvoidPage_Load(objectsender,EventArgse)
3:{
4:stringname=Request.Form["name"];
5:stringwebsite=Request.Form["website"];
6:Response.Write("你的公司:"+name+"<br/>"+"你的网址:"+website+"<br/>");
7:Response.Write("你使用的是"+Request.RequestType+"方式传送数据。");
8:Response.Write("<br/>");
9:Response.Write("当前页面调用状态:"+"IsPostBack="+IsPostBack+"。<br/>");
10:}
11:</script>
</head>
<body>
<formid="form1"runat="server">
<div>
<p>
默认原始请求(HTTPGET)方式发送数据:<br/>
</p>
<p>
你的公司<asp:TextBoxID="name"runat="server"Text="Google"></asp:TextBox><br/>
你的网站<asp:TextBoxID="website"runat="server"Text="www.google.com.hk"></asp:TextBox><br/>
</p>
<inputtype="submit"value="POST方式发送"/>
<br/>
</div>
</form>
</body>
</html>
说明:
ASP.NET默认为回发(HTTPPost)方式。
Page_Load事件添加对页面调用状态的简单判断,除了回发(HTTPPost)、原始请求(HTTPGet)外,还有跨页发送、服务器传输和回调。
用Request.Form来读取回发(HTTPPost)的数据。
ASP.NETRequest.querystring、Request.Form和Request.Params比较
Request.QueryString获取以原始请求(HTTPGet)方式提交的数据。Request.Form获取以回发(HTTPPost)方式提交的数据。
Request.Params获取所有以原始请求(HTTPGet)和回发(HTTPPost)方式提交的数据。Request.params是一个集合,它依次包括,有优先级,Request.QueryString、Request.Form、Request.cookies
和Request.ServerVariable。
Request也是获取所有以原始请求(HTTPGet)和回发(HTTPPost)方式提交的数据,获取有优先级,它会依次在QueryString、Form、ServerVariable中检索。
有时候,你会得到不同的值。如果你仅仅是想得到Form中的数据,但却用Request,而不是Request.Form,那么,因为程序将在Request.QueryString、Request.ServerVariable中也检索,要是正好遇到同名同名项,就会得到你不期望的值。
示例
示例1,演示原始请求(HTTPGet)方式向服务器端发送数据,服务器通过Request.querystring来接收。
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
<scriptrunat="server">1:
2:protectedvoidPage_Load(objectsender,EventArgse)
3:{
4:stringname=Request.QueryString["name"];
5:stringwebsite=Request.QueryString["website"];
6:Response.Write("你的公司:"+name+"<br/>"+"你的网址:"+website+"<br/>");
7:Response.Write("你使用的是"+Request.RequestType+"方式传送数据。");
8:Response.Write("<br/>");
9:Response.Write("当前页面调用状态:"+"IsPostBack="+IsPostBack+"。<br/>");
10:}
11:</script>
</head>
<body>
<formid="form1"method="get"runat="server">
<div>
<p>
原始请求(HTTPGET)方式发送数据<br/>
</p>
<p>
你的公司<asp:TextBoxID="name"runat="server"Text="Google"></asp:TextBox><br/>
你的网站<asp:TextBoxID="website"runat="server"Text="www.google.com.hk"></asp:TextBox><br/>
</p>
<inputtype="submit"value="GET方式发送"/>
<br/>
</div>
</form>
</body>
</html>
示例2。演示回发(HTTPPost)方式向服务器端发送数据,服务器通过Request.Form方式接收。
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
<scriptrunat="server">1:
2:protectedvoidPage_Load(objectsender,EventArgse)
3:{
4:stringname=Request.Form["name2"];
5:stringwebsite=Request.Form["website2"];
6:Response.Write("你的公司:"+name+"<br/>"+"你的网址:"+website+"<br/>");
7:Response.Write("你使用的是"+Request.RequestType+"方式传送数据。<br/>");
8:Response.Write("当前页面调用状态:"+"IsPostBack="+IsPostBack+"。<br/>");
9:}
10:</script>
</head>
<body>
<formid="form1"method="post"runat="server">
<div>
<p>
回发(HTTPPOST)方式发送数据:<br/>
</p>
<p>
你的公司<asp:TextBoxID="name2"runat="server"Text="Microsoft"></asp:TextBox><br/>
你的网站<asp:TextBoxID="website2"runat="server"Text="www.Microsoft.com"></asp:TextBox><br/>
</p>
<br/>
<inputtype="submit"value="Post方式发送"/>
</div>
</form>
</body>
</html>
说明:
在示例1和示例2中,如果客户端采用原始请求(HTTPGet)方式向服务器端发送数据,而服务器端通过Request.Form来获取,或是客户端采用回发(HTTPPost)方式,而服务器端通过Request.querystring,那么服务器端将无法获得客户端发送过来的数据。因为Request.querystring和Request.Form的定位很清楚。
示例3。演示服务器通过Request.Params,接收数据,客户端原始请求(HTTPGet)方式和回发(HTTPPost)方式发送过来的数据。
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
<scriptrunat="server">1:
2:protectedvoidPage_Load(objectsender,EventArgse)
3:{
4:stringname=Request.Params["name"];
5:stringwebsite=Request.Params["website"];
6:Response.Write("你的公司:"+name+"<br/>"+"你的网址:"+website+"<br/>");
7:Response.Write("你使用的是"+Request.RequestType+"方式传送数据。<br/>");
8:Response.Write("当前页面调用状态:"+"IsPostBack="+IsPostBack+"。<br/>");
9:
10://stringname=Request["name"];
11://stringwebsite=Request["website"];
12://Response.Write("你的公司:"+name+"<br/>"+"你的网址:"+website+"<br/>");
13://Response.Write("你使用的是"+Request.RequestType+"方式传送数据。<br/>");
14://Response.Write("当前页面调用状态:"+"IsPostBack="+IsPostBack+"。<br/>");
15:
16:}
17:</script>
</head>
<body>
<formid="form1"runat="server">
<div>
你的公司<asp:TextBoxID="name3"runat="server"Text="Microsoft"></asp:TextBox><br/>
<br/>
你的网站<asp:TextBoxID="website3"runat="server"Text="www.Microsoft.com"></asp:TextBox><br/>
<br/>
<br/>
<inputtype="submit"value="Get和Post方式发送"/><br/>
</div>
</form>
</body>
</html>
说明:
Request.Params既可以获得客户端以原始请求(HTTPGet)方式发送过来的数据,也可以获得以回发(HTTPPost)方式发送过来的。
示例4。演示原始请求(HTTPGET)方式发送数据到另一个页面。既可以发送数据到本页,也可以到另一个页面。当然回发(HTTPPost)也可以做到。
新建GetWayAnotherPage.aspx,如下:
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"method="get"action="GetWayAnotherPageRecive.aspx"runat="server">
<div>
<p>
原始请求(HTTPGET)方式发送数据到另一个页面:<br/>
</p>
<p>
你的公司<asp:TextBoxID="name"runat="server"Text="Google"></asp:TextBox><br/>
你的网站<asp:TextBoxID="website"runat="server"Text="www.google.com.hk"></asp:TextBox><br/>
</p>
<inputtype="submit"value="Get方式发送"/>
<br/>
</div>
</form>
</body>
</html>
新建接收数据页面GetWayAnotherPageRecive.aspx,如下:
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
<scriptrunat="server">1:
2:protectedvoidPage_Load(objectsender,EventArgse)
3:{
4:stringname=Request.QueryString["name"];
5:stringwebsite=Request.QueryString["website"];
6:Response.Write("你的公司:"+name+"<br/>"+"你的网址:"+website+"<br/>");
7:Response.Write("你使用的是"+Request.RequestType+"方式传送数据。");
8:}
9:</script>
</head>
<body>
<formid="form1"runat="server">
<div>
</div>
</form>
</body>
</html>
相关文章推荐
- ASP.NET 以 Request.Querystring、Request.Form 或 Request.Params 获取客户端的数据
- ASP.NET 以 Request.Querystring、Request.Form 或 Request.Params 获取客户端的数据
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别 [转]
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- ASP.NET-->>Get和Post的接收参数:Request.QueryString,Request.Form,Request.Params异同
- ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
- asp.net c# request.QueryString.Form遇到中文乱码的解决方法,可用
- ASP.NET 页面通过url传递参数,另一页面通过Request.QueryString获取参数为乱码问题
- asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值