i3geek.com
闫庚哲的个人博客

asp.net 服务端接收POST和GET数据全解析

提交方网页源码 Default.aspx :

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

    <title></title>

</head>

<body>

    <fieldset>

        <legend>GET提交数据</legend>

        <form id="form1" action="Accept.aspx" method="get">

        <input type="text" id="text1" name="txt1" /><br />

        <input type="text" id="text2" name="txt2" /><br />

        <input type="text" id="text3" name="txt3" /><br />

        <input type="text" id="text4" name="txt4" /><br />

        <input type="submit" id="Submit1" value="GET提交数据" />

        </form>

    </fieldset>

    <br />

    <fieldset>

        <legend>POST提交数据</legend>

        <form id="form2" action="Accept.aspx" method="post" 

accept-charset="utf-8"

>

        <input type="text" id="text5" name="txt5" /><br />

        <input type="text" id="text6" name="txt6" /><br />

        <input type="text" id="text7" name="txt7" /><br />

        <input type="text" id="text8" name="txt8" /><br />

        <input type="submit" id="Submit2" value="POST提交数据" />

        </form>

    </fieldset>

</body>

</html>

接收方源码 Accept.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

public partial class Accept : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string type = "";
        string Re = "";
        Re += "数据传送方式:";
        if (Request.RequestType.ToUpper() == "POST")
        {
            type = "POST";
            Re += type + "<br/>参数分别是:<br/>";
            SortedList table = Param();
            if (table != null)
            {
                foreach (DictionaryEntry De in table) { Re += "参数名:" + De.Key + " 值:" + De.Value + "<br/>"; }
            }
            else
            { Re = "你没有传递任何参数过来!"; }
        }
        else
        {
            type = "GET";
            Re += type + "<br/>参数分别是:<br/>";
            NameValueCollection nvc = GETInput();
            if (nvc.Count != 0)
            {
                for (int i = 0; i < nvc.Count; i++) { Re += "参数名:" + nvc.GetKey(i) + " 值:" + nvc.GetValues(i)[0] + "<br/>"; }
            }
            else
            { Re = "你没有传递任何参数过来!"; }
        }
        Response.Write(Re);
    }
    //获取GET返回来的数据
    private NameValueCollection GETInput()
    { return Request.QueryString; }
    // 获取POST返回来的数据
    private string PostInput()
    {
        try
        {
            System.IO.Stream s = Request.InputStream;
            int count = 0;
            byte[] buffer = new byte[1024];
            StringBuilder builder = new StringBuilder();
            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();
            return builder.ToString();
        }
        catch (Exception ex)
        { throw ex; }
    }
    private SortedList Param()
    {
        string POSTStr = PostInput();
        SortedList SortList = new SortedList();
        int index = POSTStr.IndexOf("&");
        string[] Arr = { };
        if (index != -1) //参数传递不只一项
        {
            Arr = POSTStr.Split('&');
            for (int i = 0; i < Arr.Length; i++)
            {
                int equalindex = Arr[i].IndexOf('=');
                string paramN = Arr[i].Substring(0, equalindex);
                string paramV = Arr[i].Substring(equalindex + 1);
                if (!SortList.ContainsKey(paramN)) //避免用户传递相同参数
                { SortList.Add(paramN, paramV); }
                else //如果有相同的,一直删除取最后一个值为准
                { SortList.Remove(paramN); SortList.Add(paramN, paramV); }
            }
        }
        else //参数少于或等于1项
        {
            int equalindex = POSTStr.IndexOf('=');
            if (equalindex != -1)
            { //参数是1项
                string paramN = POSTStr.Substring(0, equalindex);
                string paramV = POSTStr.Substring(equalindex + 1);
                SortList.Add(paramN, paramV);

            }
            else //没有传递参数过来
            { SortList = null; }
        }
        return SortList;
    }
}

实现效果如下

赞(0)
未经允许不得转载:爱上极客 » asp.net 服务端接收POST和GET数据全解析
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址