ASP.NET中Get和Post的用法

简介: 单form的提交有两种方式,一种是get的方法,一种是post 的方法.看下面代码,理解ASP.NET Get和Post两种提交的区别:                  你的名字                      你的网站                     ...

单form的提交有两种方式,一种是get的方法,一种是post 的方法.看下面代码,理解ASP.NET 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.         学习request 和 response的用法< br />  
  12.         < br />  
  13.         < br />  
  14.    < /div>  
  15. < /form>  
  16.  
  17.    
  18.  
  19. < form id="form2" method="post" runat="server">  
  20.     < div>  
  21.         你的名字< asp:TextBox ID="name2" runat="server">< /asp:TextBox>< br />  
  22.         < br />  
  23.         你的网站< asp:TextBox ID="website2" runat="server">< /asp:TextBox>< br />  
  24.         < br />  
  25.         < br />  
  26.         < asp:Button ID="Button2" runat="server" Text="send" />< br />  
  27.         < br />  
  28.         < br />  
  29.         学习request 和 response的用法< br />  
  30.         < br />  
  31.         < br />  
  32.     < /div>  
  33. < /form>  
  34.  

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

 

第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.         
  4.         string id2 = Request.Form["name2"];  
  5.         string website2 = Request.Form["website2"];  
  6.         Response.Write(id2 + "< br>" + website2);  
  7.  
  8.  
  9.         Response.Write("你使用的是" + Request.RequestType + "方式传送数据");  
  10.  
  11.     }  
  12.  
  13. string id4 = Request["name4"];  
  14.         string website4 = Request["website4"];  
  15.         Response.Write(id4 + "< br>" + website4);  
  16.  

第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);  

表单提交中,ASP.NET的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方式。

目录
相关文章
|
10月前
|
存储 开发框架 NoSQL
ASP.NET WEB——项目中Cookie与Session的用法
ASP.NET WEB——项目中Cookie与Session的用法
105 0
|
网络安全
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://xxxx.svc.cluster.local:8080/xxxx": Connection reset; nested exception is java.net.SocketException: Connection reset 什么原因导致得
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "xxxx.svc.cluster.local:8080/xxxx ": Connection reset; nested exception is java.net.SocketException: Connection reset 什么原因导致得
3162 0
|
6月前
|
数据采集 JSON API
.NET 3.5 中 HttpWebRequest 的核心用法及应用
【9月更文挑战第7天】在.NET 3.5环境下,HttpWebRequest 类是处理HTTP请求的一个核心组件,它封装了HTTP协议的细节,使得开发者可以方便地发送HTTP请求并接收响应。本文将详细介绍HttpWebRequest的核心用法及其实战应用。
245 6
|
7月前
|
API
【Azure Key Vault】.NET 代码如何访问中国区的Key Vault中的机密信息(Get/Set Secret)
【Azure Key Vault】.NET 代码如何访问中国区的Key Vault中的机密信息(Get/Set Secret)
|
7月前
|
开发框架 前端开发 .NET
闲话 ASP.NET Core 数据校验(二):FluentValidation 基本用法
闲话 ASP.NET Core 数据校验(二):FluentValidation 基本用法
|
7月前
|
开发框架 前端开发 .NET
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
|
Java 容器
RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out问题处理
讲述RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out处理方案
|
10月前
|
JavaScript
GET http://192.168.2.198:8080/sockjs-node/info?t=1626862752216 net::ERR_CONNECTION_TIMED_OUT
GET http://192.168.2.198:8080/sockjs-node/info?t=1626862752216 net::ERR_CONNECTION_TIMED_OUT
101 0
|
10月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
108 0
|
JavaScript 前端开发 API
Vue报错:sockjs.js?9be2:1627 GET http://192.168.43.88:8080/sockjs-node/info?t=1631603986586 net::ERR_CO
Vue报错:sockjs.js?9be2:1627 GET http://192.168.43.88:8080/sockjs-node/info?t=1631603986586 net::ERR_CO

热门文章

最新文章