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

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

http://blog.csdn.net/wzwlln/article/details/6180526

原创 2011年02月12日 11:34:00

ASPX文件源码:

 

 

[c-sharp] view plain copy
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head runat="server">  
  4.     <title>无标题页</title>  
  5. </head>  
  6. <body>  
  7.     <fieldset>  
  8.         <legend>GET提交数据</legend>  
  9.         <form id="form1" action="show.aspx" method="get">  
  10.         <input type="text" name="FROMCITY" value="CAN" /><br />  
  11.         <input type="text" name="TOCITY" value="SYD" /><br />  
  12.         <input type="text" name="FROMDATE" value="20110301" /><br />  
  13.         <input type="text" name="FLYTYPE" value="1" /><br />  
  14.         <br />  
  15.         <input type="submit" id="submit1" value="GET提交数据" />  
  16.         </form>  
  17.     </fieldset>  
  18.     <br />  
  19.     <fieldset>  
  20.         <legend>POST提交数据</legend>  
  21.         <form id="form2" action="show.aspx" method="post">  
  22.         <input type="text" name="FROMCITY" value="CAN" /><br />  
  23.         <input type="text" name="TOCITY" value="SYD" /><br />  
  24.         <input type="text" name="FROMDATE" value="20110301" /><br />  
  25.         <input type="text" name="FLYTYPE" value="1" /><br />  
  26.         <br />  
  27.         <input type="submit" id="submit2" value="POST提交数据" />  
  28.         </form>  
  29.     </fieldset>  
  30. </body>  
  31. </html>  

 

 

 

CS文件源码:

 

 

[c-sharp] view plain copy
 
  1. using System;  
  2. using System.Web;  
  3. using System.Web.UI;  
  4. using System.Web.UI.WebControls;  
  5. using System.Text;  
  6. using System.Collections.Specialized;  
  7. using System.Collections;  
  8.   
  9. public partial class show : System.Web.UI.Page  
  10. {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.             string type = "";  
  14.             string Re = "";  
  15.             Re += "数据传送方式:";  
  16.             if (Request.RequestType.ToUpper() == "POST")  
  17.             {  
  18.                 str = "";  
  19.                 type = "POST";  
  20.                 Re += type + "<br/>参数分别是:<br/>";  
  21.                 SortedList table = sParam();  
  22.                 //Hashtable table = hParam();  
  23.                 if (table != null)  
  24.                 {  
  25.                     foreach (DictionaryEntry De in table)  
  26.                     {  
  27.                         Re += "参数名:" + De.Key + " 值:" + De.Value + "<br/>";  
  28.                     }  
  29.                 }  
  30.                 else  
  31.                 { Re = "你没有传递任何参数过来!"; }  
  32.             }  
  33.             else  
  34.             {  
  35.                 str = "";  
  36.                 type = "GET";  
  37.                 Re += type + "<br/>参数分别是:<br/>";  
  38.                 NameValueCollection nvc = GETInput();  
  39.                 if (nvc.Count != 0)  
  40.                 {  
  41.                     for (int i = 0; i < nvc.Count; i++)  
  42.                     {  
  43.                         Re += "参数名:" + nvc.GetKey(i) + " 值:" + nvc.GetValues(i)[0] + "<br/>";  
  44.                     }  
  45.                 }  
  46.                 else  
  47.                 { Re = "你没有传递任何参数过来!"; }  
  48.             }  
  49.             Response.Write(Re);  
  50.         }  
  51.   
  52.         //获取GET返回来的数据  
  53.         private NameValueCollection GETInput()  
  54.         { return Request.QueryString; }  
  55.   
  56.         // 获取POST返回来的数据  
  57.         private string PostInput()  
  58.         {  
  59.             try  
  60.             {  
  61.                 System.IO.Stream s = Request.InputStream;  
  62.                 int count = 0;  
  63.                 byte[] buffer = new byte[1024];  
  64.                 StringBuilder builder = new StringBuilder();  
  65.                 while ((count = s.Read(buffer, 0, 1024)) > 0)  
  66.                 {  
  67.                     builder.Append(Encoding.UTF8.GetString(buffer, 0, count));  
  68.                 }  
  69.                 s.Flush();  
  70.                 s.Close();  
  71.                 s.Dispose();  
  72.                 return builder.ToString();  
  73.             }  
  74.             catch (Exception ex)  
  75.             { throw ex; }  
  76.         }  
  77.   
  78.         private Hashtable hParam()  
  79.         {  
  80.             string POSTStr = PostInput();  
  81.             Hashtable HashList = new Hashtable();  
  82.             int index = POSTStr.IndexOf("&");  
  83.             string[] Arr = { };  
  84.             if (index != -1) //参数传递不只一项  
  85.             {  
  86.                 Arr = POSTStr.Split('&');  
  87.                 for (int i = 0; i < Arr.Length; i++)  
  88.                 {  
  89.                     int equalindex = Arr[i].IndexOf('=');  
  90.                     string paramN = Arr[i].Substring(0, equalindex);  
  91.                     string paramV = Arr[i].Substring(equalindex + 1);  
  92.                     if (!HashList.ContainsKey(paramN)) //避免用户传递相同参数  
  93.                     { HashList.Add(paramN, paramV); }  
  94.                     else //如果有相同的,一直删除取最后一个值为准  
  95.                     { HashList.Remove(paramN); HashList.Add(paramN, paramV); }  
  96.                 }  
  97.             }  
  98.             else //参数少于或等于1项  
  99.             {  
  100.                 int equalindex = POSTStr.IndexOf('=');  
  101.                 if (equalindex != -1)  
  102.                 { //参数是1项  
  103.                     string paramN = POSTStr.Substring(0, equalindex);  
  104.                     string paramV = POSTStr.Substring(equalindex + 1);  
  105.                     HashList.Add(paramN, paramV);  
  106.   
  107.                 }  
  108.                 else //没有传递参数过来  
  109.                 { HashList = null; }  
  110.             }  
  111.             return HashList;  
  112.         }  
  113.   
  114.         private SortedList sParam()  
  115.         {  
  116.             string POSTStr = PostInput();  
  117.             SortedList SortList = new SortedList();  
  118.             int index = POSTStr.IndexOf("&");  
  119.             string[] Arr = { };  
  120.             if (index != -1) //参数传递不只一项  
  121.             {  
  122.                 Arr = POSTStr.Split('&');  
  123.                 for (int i = 0; i < Arr.Length; i++)  
  124.                 {  
  125.                     int equalindex = Arr[i].IndexOf('=');  
  126.                     string paramN = Arr[i].Substring(0, equalindex);  
  127.                     string paramV = Arr[i].Substring(equalindex + 1);  
  128.                     if (!SortList.ContainsKey(paramN)) //避免用户传递相同参数  
  129.                     { SortList.Add(paramN, paramV); }  
  130.                     else //如果有相同的,一直删除取最后一个值为准  
  131.                     { SortList.Remove(paramN); SortList.Add(paramN, paramV); }  
  132.                 }  
  133.             }  
  134.             else //参数少于或等于1项  
  135.             {  
  136.                 int equalindex = POSTStr.IndexOf('=');  
  137.                 if (equalindex != -1)  
  138.                 { //参数是1项  
  139.                     string paramN = POSTStr.Substring(0, equalindex);  
  140.                     string paramV = POSTStr.Substring(equalindex + 1);  
  141.                     SortList.Add(paramN, paramV);  
  142.   
  143.                 }  
  144.                 else //没有传递参数过来  
  145.                 { SortList = null; }  
  146.             }  
  147.             return SortList;  
  148.         }  
  149.     }  

 

版权声明:本文为博主原创文章,未经博主允许不得转载。
 
posted @ 2017-11-06 15:28  sky20080101  阅读(184)  评论(0)    收藏  举报