zoukankan      html  css  js  c++  java
  • asp.net中的post和get请求操作

    ASP.NET中的post和get请求操作

    主要是为了测试一下当post请求中有get参数的时候,服务器端是如何处理这两种不同的参数的。是一起当post参数处理,还是分开处理。

    客户端post_test.html

    <html>

      <head>

        Post_Get Test

      </head>

      <body>

        <form method="post" name="form" action="http://localhost:53994/post_get/Default.aspx?getvar=2">

        <input type="text" name="postvar" value="4" />

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

        </form>

      </body>

    </html>

    服务器端 Default.aspx

    <body>

        <form id="form1" runat="Server">

        <div>

            <asp:Label ID="Label4" runat="server" Text="postRequest:">

            </asp:Label><asp:Label ID="Label1" runat="server" ></asp:Label>

            <br />

            <asp:Label ID="Label5" runat="server" Text="postFormGet:">

            </asp:Label><asp:Label ID="Label2" runat="server" ></asp:Label>

            <br />

            <asp:Label ID="Label6" runat="server" Text="getQueryString:">

            </asp:Label><asp:Label ID="Label3" runat="server" ></asp:Label>

            <br />

            <asp:Label ID="Label7" runat="server" Text="getFormGet:"></asp:Label>

            <asp:Label ID="Label8" runat="server" ></asp:Label>

            <br />

            <asp:Label ID="Label9" runat="server" Text="getRequest:"></asp:Label>

            <asp:Label ID="Label10" runat="server" ></asp:Label>

        </div>

        </form>

    </body>

    服务器端Default.aspx.cs

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page

    {

        string post_name1 ;

        string post_name2;

        string get_name1;

        string get_name2;

        string get_name3;

        protected void Page_Load(object sender, EventArgs e)

        {

            FormTest();

        }

        private void FormTest()

        {

            if (Request["postvar"] != null)

            {

                post_name1 = Request["postvar"].ToString();

            }

            if (Request.Form.Get("postvar") != null)

            {

                post_name2 = Request.Form.Get("postvar").ToString();

            }

            if (Request.QueryString["getvar"] != null)

            {

                get_name1 =Request.QueryString["getvar"].ToString();

            }

            if (Request.Form.Get("getvar") != null)

            {

                get_name2 = Request.Form.Get("getvar").ToString();

            }

            if (Request["gettvar"] != null)

            {

                get_name3 = Request["gettvar"].ToString();

            }

            Label1.Text = post_name1;

            Label2.Text = post_name2;

            Label3.Text = get_name1;

            Label8.Text = get_name2;

            Label10.Text = get_name3;

        }

    }

    运行服务器端,打开post_test.html,结果如下图所示

    表明当表单提交为post且含有get参数时,Request.QueryString和Request才能获得get参数,其他方法都只能获得post参数

  • 相关阅读:
    C语言源代码——计算任何一天是星期几
    计算任意一天是星期几
    wpf利用线程制作初始界面和关闭窗体特效
    实用的 集合工具类 和 String工具类
    从“关于Java堆与栈的思考”一帖看错误信息的传播
    web.xml 配置中classpath: 与classpath*:的区别
    git的安装-环境变量配置
    解决Oracle安装时报错“SID已在使用”办法
    ORACLE日期时间函数
    Java 开发环境配置
  • 原文地址:https://www.cnblogs.com/baozi/p/3200013.html
Copyright © 2011-2022 走看看