zoukankan      html  css  js  c++  java
  • .net 接收post 的参数 加载xml

      /// <summary>
        /// SimDataLimiHandler 的摘要说明  推送
        /// </summary>
        public class SimDataLimiHandler : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                //context.Response.Write("Hello World");
                try
                {
                    if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
                    {
                        using (Stream stream = HttpContext.Current.Request.InputStream)
                        {
                            //Byte[] postBytes = new Byte[stream.Length];
                            //stream.Read(postBytes, 0, (Int32)stream.Length);
                            //string postString = Encoding.UTF8.GetString(postBytes);
    
    
                            try
                            {
                                StreamReader reader = new StreamReader(stream);
                                string jsonStr = reader.ReadToEnd();
                                jsonStr = System.Web.HttpUtility.UrlDecode(jsonStr);//url解码
                                SortedList prams = Param(jsonStr);
                                for (int i = 0; i < prams.Count; i++)
                                {
                                    Log4netHelper.Debug("解析", "参数 " + prams.GetKey(i) + "" + prams.GetByIndex(i));
                                }
                                //声明一个XMLDoc文档对象,LOAD()xml字符串  
                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml(prams.GetByIndex(0).ToString());
                                //得到XML文档根节点  
                                XmlElement root = doc.DocumentElement;
                                XmlNodeList list = root.ChildNodes;
                                foreach (XmlNode xno in list)
                                {
                                    Console.WriteLine(" 参数 " + xno.Name + "" + xno.InnerText);
    
                                }
                            }
                            catch (Exception ex)
                            {
                                Log4netHelper.Error("SimDataLimiHandler 数据解析出错", "接收推送", ex);
    
                            }
    
                        }
    
    
                    }
                }
                catch (Exception ex)
                {
                    string eventId = context.Request.Form["eventId"];
                    string data = context.Request.Form["data"];
                    Log4netHelper.Error("SimDataLimiHandler联通推送调用出错", "eventId:" + eventId + "data:" + data, ex);
                }
    
            }
    
    
    
            private SortedList Param(string POSTStr)
            {
    
                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;
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
  • 相关阅读:
    php 按照中文字母名字排序,并把相应的头像显示出来
    小程序单图上传到服务器
    小程序上传图片多图上传
    php 截取 小程序上传到服务器图片,
    小程序选中传值过去 不选中默认第一个
    小程序数据放入全局变量可以使用
    小程序用户openid设置放缓存
    小程序计算两者商家与用户之间的距离
    微擎小程序上传图片
    小程序获取用户的地理位置与商家的相距距离
  • 原文地址:https://www.cnblogs.com/lucoo/p/6184222.html
Copyright © 2011-2022 走看看