zoukankan      html  css  js  c++  java
  • ASP.NET 以 Request.Querystring、Request.Form 或 Request.Params 获取客户端的数据

    本文内容

    本文旨在说明客户端向服务器如何发送数据,以及服务器如何接收。虽然这种实现方式现在已经看不到了,但是这种机制是不变的,已经变成了一种底层实现,所以还是有必要了解的。

    • ASP.NET Get 与 Post 方式
    • ASP.NET Request.querystring、Request.Form 和 Request.Params 比较
    • 示例

    ASP.NET 原始请求(HTTP Get) 与回发(HTTP Post) 方式

    • 原始请求(HTTP Get)方式是从服务器上获取数据,主要用于查询;回发(HTTP Post)方式是向服务器传送数据,主要用于增删改。
    • 原始请求(HTTP Get)方式,以键值形式,把向服务器发送的数据添加到表单 form 的 action 属性的 URL 中。回发(HTTP Post)将表单内各个键和其值放到 HTML header 中,发送到 form 的 action 属性所指的URL,对用户隐藏。
    • 原始请求(HTTP Get)方式,服务器端通过 Request.QueryString 获取数据。而回发(HTTP Post)方式,服务器端通过 Request.Form 来获取。
    • 原始请求(HTTP Get)方式传送的数据量较小,不能大于 2KB。回发(HTTP Post)方式传送的数据量较大,默不受限制。
    • 原始请求(HTTP Get)方式安全性很低。回发(HTTP Post)方式较高。原始请求(HTTP Get)方式的安全性较回发(HTTP Post)方式要差些,包含机密信息的话,建议用回发(HTTP Post)数据提交方式。
    • 原始请求(HTTP Get)方式执行效率比回发(HTTP Post)方式好。
    • 若不指定的 form 的 method 和 action 属性,则 ASP.NET 默认为 Post 方式,则数据回发给自己。

    示例:

    <%@ Page Language="C#" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
     
        <script runat="server">
       1:  
       2:         protected void Page_Load(object sender, EventArgs e)
       3:         {
       4:             string name = Request.Form["name"];
       5:             string website = 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>
        <form id="form1" runat="server">
        <div>
            <p>
                默认原始请求(HTTP GET)方式发送数据:<br />
            </p>
            <p>
                你的公司<asp:TextBox ID="name" runat="server" Text="Google"></asp:TextBox><br />
                你的网站<asp:TextBox ID="website" runat="server" Text="www.google.com.hk"></asp:TextBox><br />
            </p>
            <input type="submit" value="POST 方式发送" />
            <br />
        </div>
        </form>
    </body>
    </html>

    说明:

    • ASP.NET 默认为回发(HTTP Post)方式。
    • Page_Load 事件添加对页面调用状态的简单判断,除了回发(HTTP Post)、原始请求(HTTP Get)外,还有跨页发送、服务器传输和回调。
    • 用 Request.Form 来读取回发(HTTP Post)的数据。

    ASP.NET Request.querystring、Request.Form 和 Request.Params 比较

    • Request.QueryString 获取以原始请求(HTTP Get)方式提交的数据。
    • Request.Form 获取以回发(HTTP Post)方式提交的数据。
    • Request.Params 获取所有以原始请求(HTTP Get)和回发(HTTP Post)方式提交的数据。Request.params 是一个集合,它依次包括,有优先级,Request.QueryString、Request.Form、Request.cookies 和 Request.ServerVariable。
    • Request 也是获取所有以原始请求(HTTP Get)和回发(HTTP Post)方式提交的数据,获取有优先级,它会依次在 QueryString、Form、ServerVariable 中检索。

    有时候,你会得到不同的值。如果你仅仅是想得到 Form 中的数据,但却用 Request,而不是 Request.Form,那么,因为程序将在 Request.QueryString、Request.ServerVariable 中也检索,要是正好遇到同名同名项,就会得到你不期望的值。

    示例

    示例1,演示原始请求(HTTP Get)方式向服务器端发送数据,服务器通过 Request.querystring 来接收。
    <%@ Page Language="C#" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
     
        <script runat="server">
       1:  
       2:         protected void Page_Load(object sender, EventArgs e)
       3:         {
       4:             string name = Request.QueryString["name"];
       5:             string website = 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>
        <form id="form1" method="get" runat="server">
        <div>
            <p>
                原始请求(HTTP GET)方式发送数据<br />
            </p>
            <p>
                你的公司<asp:TextBox ID="name" runat="server" Text="Google"></asp:TextBox><br />
                你的网站<asp:TextBox ID="website" runat="server" Text="www.google.com.hk"></asp:TextBox><br />
            </p>
            <input type="submit" value="GET 方式发送" />
            <br />
        </div>
        </form>
    </body>
    </html>
     
    示例2。演示回发(HTTP Post)方式向服务器端发送数据,服务器通过 Request.Form 方式接收。
    <%@ Page Language="C#" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
     
        <script runat="server">
       1:  
       2:         protected void Page_Load(object sender, EventArgs e)
       3:         {
       4:             string name = Request.Form["name2"];
       5:             string website = 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>
        <form id="form1" method="post" runat="server">
        <div>
            <p>
                回发(HTTP POST)方式发送数据:<br />
            </p>
            <p>
                你的公司<asp:TextBox ID="name2" runat="server" Text="Microsoft"></asp:TextBox><br />
                你的网站<asp:TextBox ID="website2" runat="server" Text="www.Microsoft.com"></asp:TextBox><br />
            </p>
            <br />
            <input type="submit" value="Post 方式发送" />
        </div>
        </form>
    </body>
    </html>

    说明:

    在示例 1 和示例 2 中,如果客户端采用原始请求(HTTP Get)方式向服务器端发送数据,而服务器端通过 Request.Form 来获取,或是客户端采用回发(HTTP Post)方式,而服务器端通过 Request.querystring,那么服务器端将无法获得客户端发送过来的数据。因为 Request.querystring 和 Request.Form 的定位很清楚。

    示例3。演示服务器通过 Request.Params,接收数据,客户端原始请求(HTTP Get)方式和回发(HTTP Post)方式发送过来的数据。
    <%@ Page Language="C#" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
     
        <script runat="server">
       1:  
       2:         protected void Page_Load(object sender, EventArgs e)
       3:         {
       4:             string name = Request.Params["name"];
       5:             string website = Request.Params["website"];
       6:             Response.Write("你的公司:" + name + "<br />" + "你的网址:" + website + "<br />");
       7:             Response.Write("你使用的是 " + Request.RequestType + " 方式传送数据。<br/>");
       8:             Response.Write("当前页面调用状态:" + "IsPostBack=" + IsPostBack + "。<br/>");
       9:  
      10:             //string name = Request["name"];
      11:             //string website = 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>
        <form id="form1" runat="server">
        <div>
            你的公司<asp:TextBox ID="name3" runat="server" Text="Microsoft"></asp:TextBox><br />
            <br />
            你的网站<asp:TextBox ID="website3" runat="server" Text="www.Microsoft.com"></asp:TextBox><br />
            <br />
            <br />
            <input type="submit" value="Get 和 Post 方式发送" /><br />
        </div>
        </form>
    </body>
    </html>

    说明:

    Request.Params 既可以获得客户端以原始请求(HTTP Get)方式发送过来的数据,也可以获得以回发(HTTP Post)方式发送过来的。

    示例4。演示原始请求(HTTP GET)方式发送数据到另一个页面。既可以发送数据到本页,也可以到另一个页面。当然回发(HTTP Post)也可以做到。

    新建 GetWayAnotherPage.aspx,如下:

    <%@ Page Language="C#" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" method="get" action="GetWayAnotherPageRecive.aspx" runat="server">
        <div>
            <p>
                原始请求(HTTP GET)方式发送数据到另一个页面:<br />
            </p>
            <p>
                你的公司<asp:TextBox ID="name" runat="server" Text="Google"></asp:TextBox><br />
                你的网站<asp:TextBox ID="website" runat="server" Text="www.google.com.hk"></asp:TextBox><br />
            </p>
            <input type="submit" value="Get 方式发送" />
            <br />
        </div>
        </form>
    </body>
    </html>

    新建接收数据页面 GetWayAnotherPageRecive.aspx,如下:

    <%@ Page Language="C#" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
     
        <script runat="server">
       1:  
       2:         protected void Page_Load(object sender, EventArgs e)
       3:         {
       4:             string name = Request.QueryString["name"];
       5:             string website = Request.QueryString["website"];
       6:             Response.Write("你的公司:" + name + "<br />" + "你的网址:" + website + "<br />");
       7:             Response.Write("你使用的是 " + Request.RequestType + " 方式传送数据。");
       8:         }
       9:     
    </script>
     
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        </div>
        </form>
    </body>
    </html>

    下载 Demo

  • 相关阅读:
    PIC32MZ tutorial -- Core Timer
    PIC32MZ tutorial -- OC Interrupt
    PIC32MZ tutorial -- External Interrupt
    PIC32MZ tutorial -- Watchdog Timer
    PIC32MZ tutorial -- Output Compare
    PIC32MZ tutorial -- Input Capture
    PIC32MZ tutorial -- 32-bit Timer
    log | logstash
    Vxlan学习笔记——原理
    python——字符串格式化
  • 原文地址:https://www.cnblogs.com/liuning8023/p/2188300.html
Copyright © 2011-2022 走看看