c# 中get和post的方法

时间:2023-03-08 23:24:37
c# 中get和post的方法

用GET发送数据则用REQUEST.QUERYSTRING[''ID'']接收

一、request.aspx

c# 中get和post的方法

二、request.aspx.cs

c# 中get和post的方法
三运行效果:

c# 中get和post的方法

以下是数据发送的途径http://localhost:1693/aspnet/request.aspx?__VIEWSTATE=%2FwEPDwUKMTYzNzQ1NDcxN2RkM6FDpEGEGWC3nc84%2FugaXC4kNns%3D&name=%E6%97%A0%E5%A3%B0%E5%B2%81%E6%9C%88&age=23&Button1=%E6%8F%90%E4%BA%A4&__EVENTVALIDATION=%2FwEWBALi0oPyDAL7uPQdAtCCr6oGAoznisYGxvbqZicMzlX%2BV0mim64FvnurygY%3D

四以下是以POST传送和以REQUEST.FORM["ID"]接收

requestpost.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="requestpost.aspx.cs" Inherits="requestpost" %>

<!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>requestpost</title>
</head>
<body>
      <form id="form2" runat="server" method="post">
    <div>
        <asp:Label ID="Label3" runat="server" Text="请输入你的姓名:"></asp:Label>
        <asp:TextBox ID="name2" runat="server"></asp:TextBox><br/>
        <asp:Label ID="Label4" runat="server" Text="请输入你的年龄:"></asp:Label>
        <asp:TextBox ID="age2" runat="server"></asp:TextBox>&nbsp;<br />
        <br/>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp;
        <asp:Button ID="Button2" runat="server" Text="提交post发送" OnClick="Button2_Click" />
    </div>
    </form>
</body>

</html>

requestpost.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class requestpost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

}
    protected void Button2_Click(object sender, EventArgs e)
    {
        string name2 = Request.Form["name2"];
        string age2 = Request.Form["age2"];
        Response.Write("你的姓名是:" + name2 + "你的年龄是:" + age2);
        Response.Write("你使用的是" + Request.RequestType + "方式传送数据");
    }
}

五、运行效果

c# 中get和post的方法

以上看到地址栏并没有长长的字符串

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

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

A 写法

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

B 写法

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