Cc example code for sending and receiving data through post

From , 5 Years ago, written in C#, viewed 217 times.
URL https://pastebin.vip/view/e2c4a40d
  1. public partial class Post_Server : System.Web.UI.Page
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.  
  6.         string type = "";
  7.         string Re = "";
  8.         Re += "数据传送方式:";
  9.         if (Request.RequestType.ToUpper() == "POST")
  10.         {
  11.             type = "POST";
  12.             Re += type + "<br/>参数分别是:<br/>";
  13.             SortedList table = Param();
  14.             if (table != null)
  15.             {
  16.                 foreach (DictionaryEntry De in table) { Re += "参数名:" + De.Key + " 值:" + De.Value + "<br/>"; }
  17.             }
  18.             else
  19.             { Re = "你没有传递任何参数过来!"; }
  20.         }
  21.         else
  22.         {
  23.             type = "GET";
  24.             Re += type + "<br/>参数分别是:<br/>";
  25.             NameValueCollection nvc = GETInput();
  26.             if (nvc.Count != 0)
  27.             {
  28.                 for (int i = 0; i < nvc.Count; i++) { Re += "参数名:" + nvc.GetKey(i) + " 值:" + nvc.GetValues(i)[0] + "<br/>"; }
  29.             }
  30.             else
  31.             { Re = "你没有传递任何参数过来!"; }
  32.         }
  33.         Response.Write(Re);
  34.  
  35.     }
  36.  
  37.     //获取GET返回来的数据
  38.     private NameValueCollection GETInput()
  39.     { return Request.QueryString; }
  40.     // 获取POST返回来的数据
  41.     private string PostInput()
  42.     {
  43.         try
  44.         {
  45.             System.IO.Stream s = Request.InputStream;
  46.             int count = 0;
  47.             byte[] buffer = new byte[1024];
  48.             StringBuilder builder = new StringBuilder();
  49.             while ((count = s.Read(buffer, 0, 1024)) > 0)
  50.             {
  51.                 builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
  52.             }
  53.             s.Flush();
  54.             s.Close();
  55.             s.Dispose();
  56.             return builder.ToString();
  57.         }
  58.         catch (Exception ex)
  59.         { throw ex; }
  60.     }
  61.     private SortedList Param()
  62.     {
  63.         string POSTStr = PostInput();
  64.         SortedList SortList = new SortedList();
  65.         int index = POSTStr.IndexOf("&");
  66.         string[] Arr = { };
  67.         if (index != -1) //参数传递不只一项
  68.         {
  69.             Arr = POSTStr.Split('&');
  70.             for (int i = 0; i < Arr.Length; i++)
  71.             {
  72.                 int equalindex = Arr[i].IndexOf('=');
  73.                 string paramN = Arr[i].Substring(0, equalindex);
  74.                 string paramV = Arr[i].Substring(equalindex + 1);
  75.                 if (!SortList.ContainsKey(paramN)) //避免用户传递相同参数
  76.                 { SortList.Add(paramN, paramV); }
  77.                 else //如果有相同的,一直删除取最后一个值为准
  78.                 { SortList.Remove(paramN); SortList.Add(paramN, paramV); }
  79.             }
  80.         }
  81.         else //参数少于或等于1项
  82.         {
  83.             int equalindex = POSTStr.IndexOf('=');
  84.             if (equalindex != -1)
  85.             { //参数是1项
  86.                 string paramN = POSTStr.Substring(0, equalindex);
  87.                 string paramV = POSTStr.Substring(equalindex + 1);
  88.                 SortList.Add(paramN, paramV);
  89.  
  90.             }
  91.             else //没有传递参数过来
  92.             { SortList = null; }
  93.         }
  94.         return SortList;
  95.     }
  96.  
  97.  
  98. }
  99.  
  100. protected void Button1_Click(object sender, EventArgs e)
  101.     {
  102.         Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
  103.         byte[] arrB = encode.GetBytes("aa=aa&bb=好飞");
  104.         HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://localhost:11626/MyTest/Post_Server.aspx");
  105.         myReq.Method = "POST";
  106.         myReq.ContentType = "application/x-www-form-urlencoded";
  107.         myReq.ContentLength = arrB.Length;
  108.         Stream outStream = myReq.GetRequestStream();
  109.         outStream.Write(arrB, 0, arrB.Length);
  110.         outStream.Close();
  111.  
  112.  
  113.         //接收HTTP做出的响应
  114.         WebResponse myResp = myReq.GetResponse();
  115.         Stream ReceiveStream = myResp.GetResponseStream();
  116.         StreamReader readStream = new StreamReader(ReceiveStream, encode);
  117.         Char[] read = new Char[256];
  118.         int count = readStream.Read(read, 0, 256);
  119.         string str = null;
  120.         while (count > 0)
  121.         {
  122.             str += new String(read, 0, count);
  123.             count = readStream.Read(read, 0, 256);
  124.         }
  125.         readStream.Close();
  126.         myResp.Close();
  127.  
  128.         Response.Write(str);
  129.     }
  130. //csharp/6479

Reply to "Cc example code for sending and receiving data through post"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website