ASP.NET接受GET和POST数据终极方法实例

    技术2025-05-01  14

    ASPX文件源码:

     

    <!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> <fieldset> <legend>GET提交数据</legend> <form id="form1" action="show.aspx" method="get"> <input type="text" name="FROMCITY" value="CAN" /><br /> <input type="text" name="TOCITY" value="SYD" /><br /> <input type="text" name="FROMDATE" value="20110301" /><br /> <input type="text" name="FLYTYPE" value="1" /><br /> <br /> <input type="submit" id="submit1" value="GET提交数据" /> </form> </fieldset> <br /> <fieldset> <legend>POST提交数据</legend> <form id="form2" action="show.aspx" method="post"> <input type="text" name="FROMCITY" value="CAN" /><br /> <input type="text" name="TOCITY" value="SYD" /><br /> <input type="text" name="FROMDATE" value="20110301" /><br /> <input type="text" name="FLYTYPE" value="1" /><br /> <br /> <input type="submit" id="submit2" value="POST提交数据" /> </form> </fieldset> </body> </html>

     

     

    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 show : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string type = ""; string Re = ""; Re += "数据传送方式:"; if (Request.RequestType.ToUpper() == "POST") { str = ""; type = "POST"; Re += type + "<br/>参数分别是:<br/>"; SortedList table = sParam(); //Hashtable table = hParam(); if (table != null) { foreach (DictionaryEntry De in table) { Re += "参数名:" + De.Key + " 值:" + De.Value + "<br/>"; } } else { Re = "你没有传递任何参数过来!"; } } else { str = ""; 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 Hashtable hParam() { string POSTStr = PostInput(); Hashtable HashList = new Hashtable(); 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 (!HashList.ContainsKey(paramN)) //避免用户传递相同参数 { HashList.Add(paramN, paramV); } else //如果有相同的,一直删除取最后一个值为准 { HashList.Remove(paramN); HashList.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); HashList.Add(paramN, paramV); } else //没有传递参数过来 { HashList = null; } } return HashList; } private SortedList sParam() { 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)