您的位置:首页 > 编程语言 > ASP

ASP.NET中get方法和post方法的区别

2013-05-31 09:56 417 查看
在网页设计中,无论是动态还是静态,get方法是默认的,它在URL地址长度是有限的,所以get请求方法能传送的数据也是有限的,一般get方法能传递256字节的数据,当get请求方法传递的数据长度不能满足需求时,就需要采用另一种请求方法post,post方法可传递的数据最大值为2mb相应地,读取post方法传递过来的数据时,需要采用form方法来获取;post方法在aspx页面执行时,地址栏看不到传送过来的参数数据,更加有利于页面的安全,所以一般情况采用post方法传送页面数据。

这里举个简单的例子:

(get方法)

html页面:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>发送GET请求</title>

</head>

<body>

<center >

发送GET请求

<hr />

<form action=default7.aspx method =get >

输入发送的内容:

<input type =text name="content1" />

<br />

<input type =submit value ="发送" />

</form>

</center>

</body>

</html>

对应的aspx页面:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>接收GET请求</title>

</head>

<body>

<center >

接收GET方法传来的内容:

<hr />

<%

    string content = Request.QueryString["content1"];

    Response.Write("GET方法发送过来的内容为:"+content);

     %>

</center>

</body>

</html>

 

(post方法)

html页面:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>发送post请求</title>

</head>

<body>

<center >

发送post请求

<hr />

<form action =default8.aspx method =post >

输入发送的内容:

<input type =text name="content1" />

<br />

<input type =submit value ="发送" />

</form>

</center>

</body>

</html>

对应的aspx页面:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>接收post请求</title>

</head>

<body>

<center >

接收post方法传来的内容:

<hr />

<%

    string content=Request .Form ["content1"];

    Response.Write("POST方法发送过来的内容为:"+content);

     %>

</center>    

</body>

</html>

 

用get方法,当执行aspx页面时,地址栏的显示有一段字符“?content1=html输入的值”

而用post方法,没显示,相比之下,post方法比较安全适用。

 

 

------------------------------------------------------------------------------------------------------------------------------------------------

 

表单form的提交有两种方式,一种是get的方法,一种是post 的方法.看下面代码,理解两种提交的区别:

 

1 <form id="form1" method="get" runat="server">
2     <div>
3          你的名字<asp:TextBox ID="name" runat="server"></asp:TextBox><br />
4         <br />
5          你的网站<asp:TextBox ID="website" runat="server"></asp:TextBox><br />
6         <br />
7         <br />
8         <asp:Button ID="Button1" runat="server" Text="send" /><br />
9         <br />
10         <br />
11          学习req
da34
uest 和 response的用法<br />
12         <br />
13         <br />
14     </div>
15     </form>

 

1 <form id="form2" method="post" runat="server">
2     <div>
3          你的名字<asp:TextBox ID="name2" runat="server"></asp:TextBox><br />
4         <br />
5          你的网站<asp:TextBox ID="website2" runat="server"></asp:TextBox><br />
6         <br />
7         <br />
8         <asp:Button ID="Button2" runat="server" Text="send" /><br />
9         <br />
10         <br />
11          学习request 和 response的用法<br />
12         <br />
13         <br />
14     </div>
15     </form>
16

 

从URL中可看出区别.那么那如何编程实现数据的接收呢?

     第1种,接收用get 方法传输的数据的写法:

1

protected void Page_Load(object sender,
EventArgs e)
2

    {
3

        string id = Request.QueryString["name"];
4

        string website = Request.QueryString["website"];
5

         Response.Write(id + "<br>" + website);
6


7

       Response.Write("你使用的是" + Request.RequestType + "方式传送数据");
8


9

     }
10


 

    第2种,接收用post 方法传输的数据的写法:

1 protected void Page_Load(object sender, EventArgs e)
2      {      
3         string id2 = Request.Form["name2"];
4         string website2 = Request.Form["website2"];
5          Response.Write(id2 + "<br>" + website2);
6          Response.Write("你使用的是" + Request.RequestType + "方式传送数据");

8      }
9

      第3种,同时接受get 和post 方法传送数据的代码写法:

A 写法

1 string id3 = Request.Params["name3"];
2         string website3 = Request.Params["website3"];
3          Response.Write(id3 + "<br>" + website3);
4

 

B 写法

1 string id4 = Request["name4"];
2         string website4 = Request["website4"];
3          Response.Write(id4 + "<br>" + website4);

 

表单提交中get和post方式的区别归纳如下几点:

1. get是从服务器上获取数据,post是向服务器传送数据。

2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。

3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。

4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。

5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。 

建议:

1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;

2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: